Home > OS >  change the dataframe in pyrhon instead of column value as an own column
change the dataframe in pyrhon instead of column value as an own column

Time:12-20

I have a dataframe like this: df

Country Year Area count
Albania 2018 T 250
Albania 2018 NT 150
Albania 2019 T 260
Albania 2019 NT 180
USA 2018 T 200
USA 2018 NT 120
USA 2019 T 280
USA 2019 NT 180
Oman 2018 T 260
Oman 2018 NT 160
Oman 2019 T 220
Oman 2019 NT 120
UK 2018 T 290
UK 2018 NT 140
UK 2019 T 290
UK 2019 NT 130

But I want these ouput

Country Year T NT
Albania 2018 250 150
Albania 2019 260 180
USA 2018 200 120
USA 2019 280 180
Oman 2018 260 160
Oman 2019 220 120
UK 2018 290 140
UK 2019 290 130

Is there a way to put the column values into columns?

CodePudding user response:

You can use crosstab:

dfx=pd.crosstab([df['Country'],df['Year']], df['Area'],values=df['count'],aggfunc='first').reset_index()

Output:

Area  Country  Year   NT    T
0     Albania  2018  150  250
1     Albania  2019  180  260
2        Oman  2018  160  260
3        Oman  2019  120  220
4          UK  2018  140  290
5          UK  2019  130  290
6         USA  2018  120  200
7         USA  2019  180  280

CodePudding user response:

An alternative is to use the pivot() method. For example,

df.pivot(index=['Country', 'Year'], columns='Area', values='count').reset_index(level=1)

Output

Area     Year   NT    T
Country
Albania  2018  150  250
Albania  2019  180  260
Oman     2018  160  260
Oman     2019  120  220
UK       2018  140  290
UK       2019  130  290
USA      2018  120  200
USA      2019  180  280
  • Related