Home > Blockchain >  make a dataframe base on a rows and columns of another dataframe
make a dataframe base on a rows and columns of another dataframe

Time:06-17

i have a a dataframe 'a' like this:

A header c1 c2 c3 c4 c5
c1 0 1 3 4 2
c2 2 4 3 2 4
c3 2 4 1 2 2
c4 8 4 6 5 7
c5 7 4 5 8 9

now, i want to make a dataframe based on previous dataframe with name 'b'.

column1 column2 column3
c1 c1 0
c1 c2 1
c1 c3 3
c1 c4 4
c1 c5 2
c2 c1 2
c2 c2 4
c2 c3 3
c2 c4 2
c2 c5 4
c3 c1 2
c3 c2 4
c3 c3 1
c3 c4 2
c3 c5 2
c4 c1 8
c4 c2 4
c4 c3 6
c4 c4 5
c4 c5 7
c5 c1 7
c5 c2 4
c5 c3 5
c5 c4 8
c5 c5 9

could you help me to make this second dataframe ? thanks

CodePudding user response:

You can try this:

a.stack().reset_index()

CodePudding user response:

You can use the .melt function as follow:

pd.melt(df1, id_vars=['A header'], value_vars=['c1', 'c2','c3','c4','c5'])

you can check the documentation here : https://pandas.pydata.org/docs/reference/api/pandas.melt.html

  • Related