连接并连接一系列 NaN 的结果
Join and concat results on series of NaNs
我需要帮助将两个数据帧组合在一起:
df1 = pd.DataFrame({'A': [100, 120, 150, 130]}, index=[2, 4, 5, 6])
df2 = pd.DataFrame({'X': [200,230,210,220,245,260], Y: [300,330,300,360,310,390]}, index=[1,2,3,4,5,6])
我需要获得
df3 =
index X Y A
2 230 330 100
4 220 360 120
5 245 310 150
6 260 390 130
但是,当我使用 concat([df2,df1],axis=1)` 时,我在 A 列上得到了一堆 NaN:
print (pd.concat([df2,df1],axis=1))
X Y A
1 200 300 NaN
2 230 330 NaN
3 210 300 NaN
4 220 360 NaN
5 245 310 NaN
6 260 390 NaN
解决这个问题的最佳方法是什么?
这是预期的,因为 DataFrame
按 concat
or join
中的索引值对齐:
print (pd.concat([df2,df1],axis=1))
X Y A
1 200 300 NaN
2 230 330 100.0 <-index 2 from df2 is aligned with row with index 2 in df1
3 210 300 NaN
4 220 360 120.0
5 245 310 150.0
6 260 390 130.0
编辑:
您的索引值似乎有不同 dtype
s:
df2.index = df2.index.astype(str)
print (df1.index)
Int64Index([2, 4, 5, 6], dtype='int64')
print (df2.index)
Index(['1', '2', '3', '4', '5', '6'], dtype='object')
print (pd.concat([df2,df1],axis=1))
X Y A
1 200.0 300.0 NaN
2 230.0 330.0 NaN
3 210.0 300.0 NaN
4 220.0 360.0 NaN
5 245.0 310.0 NaN
6 260.0 390.0 NaN
2 NaN NaN 100.0
4 NaN NaN 120.0
5 NaN NaN 150.0
6 NaN NaN 130.0
解决方案是转换索引值:
df2.index = df2.index.astype(int)
print (pd.concat([df2,df1],axis=1))
X Y A
1 200 300 NaN
2 230 330 100.0
3 210 300 NaN
4 220 360 120.0
5 245 310 150.0
6 260 390 130.0
我需要帮助将两个数据帧组合在一起:
df1 = pd.DataFrame({'A': [100, 120, 150, 130]}, index=[2, 4, 5, 6])
df2 = pd.DataFrame({'X': [200,230,210,220,245,260], Y: [300,330,300,360,310,390]}, index=[1,2,3,4,5,6])
我需要获得 df3 =
index X Y A
2 230 330 100
4 220 360 120
5 245 310 150
6 260 390 130
但是,当我使用 concat([df2,df1],axis=1)` 时,我在 A 列上得到了一堆 NaN:
print (pd.concat([df2,df1],axis=1))
X Y A
1 200 300 NaN
2 230 330 NaN
3 210 300 NaN
4 220 360 NaN
5 245 310 NaN
6 260 390 NaN
解决这个问题的最佳方法是什么?
这是预期的,因为 DataFrame
按 concat
or join
中的索引值对齐:
print (pd.concat([df2,df1],axis=1))
X Y A
1 200 300 NaN
2 230 330 100.0 <-index 2 from df2 is aligned with row with index 2 in df1
3 210 300 NaN
4 220 360 120.0
5 245 310 150.0
6 260 390 130.0
编辑:
您的索引值似乎有不同 dtype
s:
df2.index = df2.index.astype(str)
print (df1.index)
Int64Index([2, 4, 5, 6], dtype='int64')
print (df2.index)
Index(['1', '2', '3', '4', '5', '6'], dtype='object')
print (pd.concat([df2,df1],axis=1))
X Y A
1 200.0 300.0 NaN
2 230.0 330.0 NaN
3 210.0 300.0 NaN
4 220.0 360.0 NaN
5 245.0 310.0 NaN
6 260.0 390.0 NaN
2 NaN NaN 100.0
4 NaN NaN 120.0
5 NaN NaN 150.0
6 NaN NaN 130.0
解决方案是转换索引值:
df2.index = df2.index.astype(int)
print (pd.concat([df2,df1],axis=1))
X Y A
1 200 300 NaN
2 230 330 100.0
3 210 300 NaN
4 220 360 120.0
5 245 310 150.0
6 260 390 130.0