Home > database >  How to convert row value to new columns with boolean value in panda? - python
How to convert row value to new columns with boolean value in panda? - python

Time:01-05

The table I have right now:

Name Year
A 2016
B 2017
C 2018
D 2019

The table I want to look like:

Name 2016 2017 2018 2019
A YES No No No
B No YES No No
C No No YES No
D No No No YES

CodePudding user response:

This works:

import pandas as pd
import numpy as np

data = [
    {'Name': 'A', 'Year': 2016},
    {'Name': 'B', 'Year': 2017},
    {'Name': 'C', 'Year': 2018},
    {'Name': 'D', 'Year': 2019},
]

df = pd.DataFrame(data)

out = (pd.DataFrame(np.identity(df.shape[0]), 
                index=df['Name'], 
                columns=df['Year'])
      .replace({1:"Yes",0:"No"})
)

CodePudding user response:

this is an option - @user20843299's solution is more efficient - less number of calls:

(df
.assign(num=1)
.pivot(index='Name', columns='Year', values='num')
.replace({1:"Yes", np.nan:"No"})
.rename_axis(columns=None)
.reset_index()
) 
  Name 2016 2017 2018 2019
0    A  Yes   No   No   No
1    B   No  Yes   No   No
2    C   No   No  Yes   No
3    D   No   No   No  Yes
  • Related