0

For example, I have follow pandas DataFrame:

import pandas as pd

df = pd.DataFrame(data=[[1, 2, 3], [4, 5, 6]], columns=['a', 'b', 'c'])
print(df)

   a  b  c
0  1  2  3
1  4  5  6

I want to convert it into below format:

  field data
0     a    1
1     a    4
2     b    2
3     b    5
4     c    3
5     c    6

The original column name as the new colume field value, and the data is the original data of column, how to implement this?

Felix Du
  • 105
  • 6

1 Answers1

2

You can stack the datframe, then perform some index drop/reset, and column renames:

df.stack().droplevel(0).reset_index().rename(columns={'index': 'field', 0:'data'})

OUTPUT

  field  data
0     a     1
1     b     2
2     c     3
3     a     4
4     b     5
5     c     6

Or you can just use melt method passing variable and value column names:

>>> df.melt(var_name='field', value_name='data')

  field  data
0     a     1
1     a     4
2     b     2
3     b     5
4     c     3
5     c     6

The order of the values differ for stack and melt though.

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45