Reputation: 79
Lets assume 2 dataframes (float type), with same index/number of rows and dynamic number of columns, generated by some function.
df1 df2
0 avg 0 1 avg
0 1.1 1.1 0 2.1 2.4 2.25
1 1.2 1.2 1 2.2 2.5 2.35
2 1.3 1.3 2 2.3 2.6 2.45
I would like to get from each df, all columns except the last, and append them to a new df in an iterative way.
i have tried the following
df_lst=[df1, df2]
total_df=pd.DataFrame()
for frame in df_lst:
total_df=pd.concat(total_df, frame.iloc[:, 0:-1])
but for some reason new_df has NaN values for the last columns appended
can someone explain why is this happening? And how could i could solve it?( I have also tried with join, append, assign, but then all columns in new_df were empty)
desired output would look like
new_df
0 1.1 2.1 2.4
1 1.2 2.2 2.5
2 1.3 2.3 2.6
Upvotes: 0
Views: 477
Reputation: 150745
Just add axis into concat
:
df_list = [df1,df2]
pd.concat([d.iloc[:, :-1] for d in df_list], axis=1)
Output:
0 0 1
0 1.1 2.1 2.4
1 1.2 2.2 2.5
2 1.3 2.3 2.6
Upvotes: 1