Python 熊猫的数据帧/行索引

Python 熊猫的数据帧/行索引,python,python-3.x,pandas,matplotlib,Python,Python 3.x,Pandas,Matplotlib,我想知道如何索引数据集,以便df1的行号可以等于df2的不同行号?例如,df 1中的第1行=df2中的第3行 2011年第49:50行b1与2016年第51:52行bt相同(都是相同的项目,但不同年份的值不同),但由于2016年在不同的单元格中,所以切片方式不同 我一直在使用pd.concat和pd.Series,但仍然没有成功 # slicing 2011 data (total) b1 = df1.iloc[49:50, 6:7] m1 = df1.iloc[127:12

我想知道如何索引数据集,以便df1的行号可以等于df2的不同行号?例如,df 1中的第1行=df2中的第3行

2011年第49:50行b1与2016年第51:52行bt相同(都是相同的项目,但不同年份的值不同),但由于2016年在不同的单元格中,所以切片方式不同

我一直在使用
pd.concat
pd.Series
,但仍然没有成功

# slicing 2011 data (total)
b1 = df1.iloc[49:50, 6:7]         
m1 = df1.iloc[127:128, 6:7]   
a1 = df1.iloc[84:85, 6:7]  

data2011 = pd.concat([b1, m1, a1])

# slicing 2016 data (total)
bt = df2.iloc[51:52, 6:7]        
mt = df2.iloc[129:130, 6:7]   
at = df2.iloc[86:87, 6:7]     

data2016 = pd.concat([bt, mt, at])

data20112016 = pd.concat([data2011, data2016])

print(data20112016)
我得到的输出:

我想做一个比较b12011和bt2016的条形图,以此类推。意思是42=51,127=129等

#           Tot_x  Tot_y
# 49=51     11849  13500
# 127=129   22622  25281
# 84=86     13658  18594
我希望这能把事情弄清楚


提前感谢。

如果我正确理解了您的问题,下面是使用
合并的解决方案:

df1 = pd.DataFrame([9337, 2953, 8184], index=[49, 127, 84], columns=['Tot'])
df2 = pd.DataFrame([13500, 25281, 18594], index=[51, 129, 86], columns=['Tot'])

total_df = (df1.reset_index()
               .merge(df2.reset_index(), left_index=True, right_index=True))
下面是,使用
concat

total_df = pd.concat([df1.reset_index(), df2.reset_index()], axis=1)
下面是结果条形图:

total_df.index = total_df['index_x'].astype(str) + '=' + total_df['index_y'].astype(str)
total_df
#          index_x  Tot_x  index_y  Tot_y
#   49=51       49   9337       51  13500
# 127=129      127   2953      129  25281
#   84=86       84   8184       86  18594

(total_df.drop(['index_x', 'index_y'], axis=1)
         .plot(kind='bar', rot=0))

请添加可复制的示例,不要使用屏幕截图。安东尼,请添加预期输出。完成。希望这能让一切变得更清楚。更新我的答案。看起来,这就是你们想要的结果。不管怎样,我可以重命名49=51,127=129和84=86。感谢您迄今为止的工作,非常感谢@Anthony.H是的,您可以更改数据帧索引,例如
df.index=iterable
我假设我不能对图形使用iloc[slicing],我必须硬编码?@Anthony.H为什么不能使用slicing?我指的是df1=pd.dataframe([933729538184],index=[49127,84],columns=[Tot'])如果我使用切片,我将如何适应它?请参阅我问题中的切片iloc数据。干杯
total_df.index = total_df['index_x'].astype(str) + '=' + total_df['index_y'].astype(str)
total_df
#          index_x  Tot_x  index_y  Tot_y
#   49=51       49   9337       51  13500
# 127=129      127   2953      129  25281
#   84=86       84   8184       86  18594

(total_df.drop(['index_x', 'index_y'], axis=1)
         .plot(kind='bar', rot=0))