Home > Net >  how to append columns in dataframe
how to append columns in dataframe

Time:03-31

I have a dataframe like this,

data = {'first_column':  ['first_sentence', 'second_sentence'], 'second_column': ['A', 'B'],         'third_column' : ['C', 'D'] }

The original structure like this

first_column second_column third_column

first_sentence A C

second_sentence B D

I want to convert it into following format

first_column column

first_sentence A

first_sentence C

second_sentence B

second_sentence D

I tried df.stack()

but it gives

0 first_column first_sentence

second_column A

third_column C

1 first_column second_sentence

second_column B

third_column D

I have around 27 columns in original dataframe and i want to produce 1 column, how can i do this

thank you in advance.

CodePudding user response:

Use set_index before stack:

out= df.set_index('first_column').stack().droplevel(1).rename('column').reset_index()
print(out)

# Output
      first_column column
0   first_sentence      A
1   first_sentence      C
2  second_sentence      B
3  second_sentence      D

CodePudding user response:

Try to use melt:

pd.melt(df, id_vars=['first_column'], value_vars=['second_column', 'third_column'], value_name='column')
        .sort_values(by="first_column").drop(['variable'], axis=1)

enter image description here

  • Related