Mostafa Mohamed
Mostafa Mohamed

Reputation: 11

python pandas change columns to rows

I have this data frame using python and this is the result I wanted after cleaned and analyzing the data

result from python

but I wanted in this format how can I do this in pandas python formt I eant

Upvotes: 0

Views: 31

Answers (1)

ragas
ragas

Reputation: 916

Hopefully this is what you are looking for.

df = pd.DataFrame({'status': ['cancelled', 'failed', 'pending', 'ticketed'],
                   'new': [511, 5561, 105, 2115],
                   'atc': [97, 580, 10, 646]})



df1 = pd.melt(df, id_vars=['status'], value_vars=['new', 'atc'], value_name='value' ).sort_values('status')

print(df1)



   status variable  value
0  cancelled      new    511
4  cancelled      atc     97
1     failed      new   5561
5     failed      atc    580
2    pending      new    105
6    pending      atc     10
3   ticketed      new   2115
7   ticketed      atc    646

Upvotes: 1

Related Questions