Pandas: data concatenation frame with a different column name

Suppose I have this data frame

id xy 0 a hello 0 b test 1 c hi 2 a hi 3 d bar 

I want to concatenate x and y into one column like this, keeping my identifiers

 id x 0 a 0 b 1 c 2 a 3 d 0 hello 0 test 1 hi 2 hi 3 bar 

And what if I want to give a new name for the concat column? (e.g. 'x' to 'xy')

+2
source share
2 answers

I don't think pandas.concat includes the ability to set new column names (see docs) , but you can assign like this:

Beginning with:

  id xy 0 0 a hello 1 0 b test 2 1 c hi 3 2 a hi 4 3 d bar df.set_index('id', inplace=True) pd.DataFrame(pd.concat([df.x, df.y]), columns=['xy']).reset_index() id xy 0 0 a 1 0 b 2 1 c 3 2 a 4 3 d 5 0 hello 6 0 test 7 1 hi 8 2 hi 9 3 bar 
+1
source

If the order of the lines is not important, you can use stack :

 print df id xy 0 0 a hello 1 0 b test 2 1 c hi 3 2 a hi 4 3 d bar s = df.set_index('id').stack() s.index = s.index.droplevel(-1) s.name = 'xy' print pd.DataFrame(s).reset_index() id xy 0 0 a 1 0 hello 2 0 b 3 0 test 4 1 c 5 1 hi 6 2 a 7 2 hi 8 3 d 9 3 bar 
+1
source

Source: https://habr.com/ru/post/1261509/


All Articles