Python pandas concat 生成 nan 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40339886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas concat generates nan values
提问by Georg Heiler
I am curious why a simple concatenation of two data frames in pandas:
我很好奇为什么在熊猫中简单地连接两个数据框:
shape: (66441, 1)
dtypes: prediction int64
dtype: object
isnull().sum(): prediction 0
dtype: int64
shape: (66441, 1)
CUSTOMER_ID int64
dtype: object
isnull().sum() CUSTOMER_ID 0
dtype: int64
of the same shape and both without NaN values
形状相同且都没有 NaN 值
foo = pd.concat([initId, ypred], join='outer', axis=1)
print(foo.shape)
print(foo.isnull().sum())
can result in a lot of NaN values if joined.
如果加入,可能会产生很多 NaN 值。
(83384, 2)
CUSTOMER_ID 16943
prediction 16943
How can I fix this problem and prevent NaN values being introduced?
如何解决此问题并防止引入 NaN 值?
Trying to reproduce it like
试图重现它
aaa = pd.DataFrame([0,1,0,1,0,0], columns=['prediction'])
print(aaa)
bbb = pd.DataFrame([0,0,1,0,1,1], columns=['groundTruth'])
print(bbb)
pd.concat([aaa, bbb], axis=1)
failed e.g. worked just fine as no NaN values were introduced.
失败,例如工作得很好,因为没有引入 NaN 值。
回答by jezrael
I think there is problem with different index values, so where concat
cannot align get NaN
:
我认为不同的索引值有问题,所以哪里concat
不能对齐 get NaN
:
aaa = pd.DataFrame([0,1,0,1,0,0], columns=['prediction'], index=[4,5,8,7,10,12])
print(aaa)
prediction
4 0
5 1
8 0
7 1
10 0
12 0
bbb = pd.DataFrame([0,0,1,0,1,1], columns=['groundTruth'])
print(bbb)
groundTruth
0 0
1 0
2 1
3 0
4 1
5 1
print (pd.concat([aaa, bbb], axis=1))
prediction groundTruth
0 NaN 0.0
1 NaN 0.0
2 NaN 1.0
3 NaN 0.0
4 0.0 1.0
5 1.0 1.0
7 1.0 NaN
8 0.0 NaN
10 0.0 NaN
12 0.0 NaN
Solution is reset_index
if indexes values are not necessary:
解决方案是reset_index
如果不需要索引值:
aaa.reset_index(drop=True, inplace=True)
bbb.reset_index(drop=True, inplace=True)
print(aaa)
prediction
0 0
1 1
2 0
3 1
4 0
5 0
print(bbb)
groundTruth
0 0
1 0
2 1
3 0
4 1
5 1
print (pd.concat([aaa, bbb], axis=1))
prediction groundTruth
0 0 0
1 1 0
2 0 1
3 1 0
4 0 1
5 0 1