pandas concat generates nan values
Asked Answered
P

4

88

I am curious why a simple concatenation of two dataframes in pandas:

initId.shape                # (66441, 1)
initId.isnull().sum()       # 0

ypred.shape                 # (66441, 1)
ypred.isnull().sum()        # 0

of the same shape and both without NaN values

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

can result in a lot of NaN values if joined.

How can I fix this problem and prevent NaN values being introduced? Trying to reproduce it like

aaa  = pd.DataFrame([0,1,0,1,0,0], columns=['prediction'])
bbb  = pd.DataFrame([0,0,1,0,1,1], columns=['groundTruth'])
pd.concat([aaa, bbb], axis=1)

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

Procedure answered 31/10, 2016 at 9:55 Comment(0)
S
124

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

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

EDIT: 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
Scavenge answered 31/10, 2016 at 9:58 Comment(0)
J
5

You can do something like this:

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)

To concatenate multiple DataFrames and keep the columns names / avoid NaN.

Jugulate answered 24/10, 2020 at 1:37 Comment(0)
R
1

As jezrael pointed out, this is due to different index labels. concat matches on index, so if they are not the same, this problem will occur. For a straightforward horizontal concatenation, you must "coerce" the index labels to be the same. One way is via set_axis method. This makes the second dataframes index to be the same as the first's.

joined_df = pd.concat([df1, df2.set_axis(df1.index)], axis=1)

or just reset the index of both frames

joined_df = pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], axis=1)
Reinwald answered 14/2, 2023 at 17:53 Comment(0)
L
0

If the data frames have the same number of rows you can transpose the original dataframes and then concatenate row-wise to avoid issues with indexes. This approach would be beneficial if you have a lot of dataframes to concatenate.

dataframes_list = [df1, df2, df3]
dataframes_list_transposed = [list_item.transpose() for list_item in dataframes_list]
merged_dataframe = pd.concat(dataframes_list_transposed, axis=0).transpose()
Lowbrow answered 2/1 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.