Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将dataframe与另一个dataframe的前2列合并而不更改索引位置_Python_Pandas_Dataframe_Indexing - Fatal编程技术网

Python 将dataframe与另一个dataframe的前2列合并而不更改索引位置

Python 将dataframe与另一个dataframe的前2列合并而不更改索引位置,python,pandas,dataframe,indexing,Python,Pandas,Dataframe,Indexing,我最初有一个'monthly'数据框,其中月份(1-11)作为列索引,疾病病例数作为值。 我还有另一个'disease'数据框,前两列是'Country'和'Province'。 我想将'monthly'数据框与两列合并,这两列仍然应该是组合的'monthly'数据框中的前两列(相同的索引位置) 换句话说,原始的“每月”数据帧是: 1 2 3 4 5 6 7 8 9 10 11 0 1 5 8 0 9 9 8

我最初有一个
'monthly'数据框
,其中月份(1-11)作为列索引,疾病病例数作为值。 我还有另一个
'disease'数据框
,前两列是
'Country'
'Province'
。 我想将
'monthly'数据框
与两列合并,这两列仍然应该是组合的
'monthly'数据框
中的前两列(相同的索引位置)

换句话说,原始的“每月”数据帧是:

     1   2   3   4   5   6   7   8    9    10   11
 0   1   5   8   0   9   9   8   18   82   89   81
 1   0   1   9   19  8   12  29  19   91   74   93
所需输出为:

    Country       Province    1   2   3   4   5   6   7   8    9    10   11
 0  Afghanistan  Afghanistan  1   5   8   0   9   9   8   18   82   89   81
 1  Argentina    Argentina    0   1   9   19  8   12  29  19   91   74   93
我能够通过以下代码将这两列附加到
“每月”数据框中

monthly['Country'] = disease['Country']
monthly['Province'] = disease['Province']
但是,这会将两列放在
“每月”数据框的末尾

      1   2   3   4   5   6   7   8    9    10   11   Country       Province
 0    1   5   8   0   9   9   8   18   82   89   81   Afghanistan  Afghanistan
 1    0   1   9   19  8   12  29  19   91   74   93   Argentina    Argentina
如何在不使用insert()函数的情况下改进代码?我可以使用
iloc
指定索引位置吗

提前谢谢你的帮助

用于按位置选择前两列,此处first
表示获取所有行:

df = pd.concat((disease.iloc[:, :2], monthly), axis=1)
或按列名称:

df = pd.concat((disease[['Country','Province']], monthly), axis=1)

pd.concat((疾病,每月),axis=1)
?使用
pd.concat((疾病,iloc[:,:2],每月),axis=1)
pd.concat((疾病[[国家],省],每月),axis=1)
,这是一个清楚的解释。非常感谢你!