pandas concat 生成 nan 值
pandas concat generates nan values
我很好奇为什么 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 值
foo = pd.concat([initId, ypred], join='outer', axis=1)
print(foo.shape)
print(foo.isnull().sum())
如果加入,可能会产生很多 NaN 值。
(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 值。
我认为不同索引值存在问题,所以 concat
无法对齐得到 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
:
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
相同的索引并且数据帧的长度相同,请使用:
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
你可以这样做:
concatenated_dataframes = concat(
[
dataframe_1.reset_index(drop=True),
dataframe_2.reset_index(drop=True),
dataframe_3.reset_index(drop=True)
],
axis=1,
ignore_index=True,
)
concatenated_dataframes_columns = [
list(dataframe_1.columns),
list(dataframe_2.columns),
list(dataframe_3.columns)
]
flatten = lambda nested_lists: [item for sublist in nested_lists for item in sublist]
concatenated_dataframes.columns = flatten(concatenated_dataframes_columns)
连接多个 DataFrame
并保留列名/避免 NaN
.
我很好奇为什么 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 值
foo = pd.concat([initId, ypred], join='outer', axis=1)
print(foo.shape)
print(foo.isnull().sum())
如果加入,可能会产生很多 NaN 值。
(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 值。
我认为不同索引值存在问题,所以 concat
无法对齐得到 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
:
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
相同的索引并且数据帧的长度相同,请使用:
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
你可以这样做:
concatenated_dataframes = concat(
[
dataframe_1.reset_index(drop=True),
dataframe_2.reset_index(drop=True),
dataframe_3.reset_index(drop=True)
],
axis=1,
ignore_index=True,
)
concatenated_dataframes_columns = [
list(dataframe_1.columns),
list(dataframe_2.columns),
list(dataframe_3.columns)
]
flatten = lambda nested_lists: [item for sublist in nested_lists for item in sublist]
concatenated_dataframes.columns = flatten(concatenated_dataframes_columns)
连接多个 DataFrame
并保留列名/避免 NaN
.