pandas concat 生成 nan 值 [英] pandas concat generates nan values

查看:77
本文介绍了pandas concat 生成 nan 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇为什么在 Pandas 中简单地连接两个数据框:

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

形状相同且都没有 NaN 值

of the same shape and both without NaN values

foo = pd.concat([initId, ypred], join='outer', axis=1)
print(foo.shape)
print(foo.isnull().sum())

如果加入,可能会产生很多 NaN 值.

can result in a lot of NaN values if joined.

(83384, 2)
CUSTOMER_ID    16943
prediction     16943

如何解决此问题并防止引入 NaN 值?

尝试复制它像

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)

失败例如由于没有引入 NaN 值,因此工作正常.

failed e.g. worked just fine as no NaN values were introduced.

推荐答案

我认为不同索引值有问题,所以 concat 不能对齐 get NaN:

I think there is problem with different index values, so where concat cannot align 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

解决方案是reset_index 如果不需要索引值:

Solution is reset_index if indexes values are not necessary:

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

如果需要与 aaa 相同的索引并且 DataFrames 的长度相同,请使用:

If need same index like aaa and length of DataFrames is same use:

bbb.index = aaa.index
print (pd.concat([aaa, bbb], axis=1))
    prediction  groundTruth
4            0            0
5            1            0
8            0            1
7            1            0
10           0            1
12           0            1

这篇关于pandas concat 生成 nan 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆