0

I have many dataframes that I want to combine. I only need 1 "level_0" column.

pd.concat([df_NB_E, df_LDA_E, df_DT_E, df_RF_E], axis=1)

enter image description here

2
  • If all level_0 columns are identical, you can delete them. Though not by name, by position (via .iloc)! Commented Nov 2, 2016 at 17:00
  • Otherwise merge repeatedly. Commented Nov 2, 2016 at 17:21

2 Answers 2

1

it seems, that level_0 could be your index, right?

you have:

>>> level0 = ['ALL','AWA','REM','S1','S2','SWS']
>>> df1 = pd.DataFrame(data={'level_0':level0, 'col1':np.random.randint(0,9,6)})
>>> df2 = pd.DataFrame(data={'level_0':level0, 'col2':np.random.randint(0,9,6)})
>>> df3 = pd.DataFrame(data={'level_0':level0, 'col3':np.random.randint(0,9,6)})

>>> df1
   col1 level_0
0     5     ALL
1     8     AWA
2     5     REM
3     3      S1
4     8      S2
5     4     SWS

>>> df2
   col2 level_0
0     4     ALL
1     1     AWA
2     3     REM
3     2      S1
4     5      S2
5     1     SWS

>>> df3
   col3 level_0
0     1     ALL
1     3     AWA
2     0     REM
3     4      S1
4     2      S2
5     3     SWS

>>> pd.concat([df1,df2,df3], axis=1)

   col1 level_0  col2 level_0  col3 level_0
0     5     ALL     4     ALL     1     ALL
1     8     AWA     1     AWA     3     AWA
2     5     REM     3     REM     0     REM
3     3      S1     2      S1     4      S1
4     8      S2     5      S2     2      S2
5     4     SWS     1     SWS     3     SWS

you can set level_0 as your index, then concatenate:

>>> pd.concat([df1.set_index('level_0'), df2.set_index('level_0'), df3.set_index('level_0')], axis=1)

         col1  col2  col3
level_0                  
ALL         5     4     1
AWA         8     1     3
REM         5     3     0
S1          3     2     4
S2          8     5     2
SWS         4     1     3

of if it's not an index, you can remove it before concat:

>>> pd.concat([df1.drop('level_0', axis=1), df2.drop('level_0', axis=1), df3.drop('level_0', axis=1)], axis=1)

   col1  col2  col3
0     5     4     1
1     8     1     3
2     5     3     0
3     3     2     4
4     8     5     2
5     4     1     3
Sign up to request clarification or add additional context in comments.

Comments

0

With these command I was able to delete all the columns with name "level_0"

df.drop(df.columns[[0]], axis=1, inplace=True) 
df

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.