i have I have a table that I load as a dataframe with the help of a pandas. Next, I would like to create a PivotTable table:
Name | Lang | skills |
---|---|---|
Michael | http | 1 |
Cristiano | css | 2 |
John | js | 3 |
Piter | http | 4 |
Michael | css | 3 |
Cristiano | js | 2 |
John | http | 1 |
Piter | css | 4 |
NaN | js | 1 |
I use the command
pivot = pd.pivot_table(df, values= 'skills', index = 'Name', columns= 'Lang', aggfunc ='sum')
And I have:
Name | css | http | js |
---|---|---|---|
Cristiano | 2 | nan | 2 |
John | nan | 1 | 3 |
Michael | 3 | 1 | nan |
Piter | 4 | 4 | nan |
The problem is, I would like the Empty index to be included as well Thank You!
CodePudding user response:
Replace nan
with string like 'Other', then do the operation:
import pandas as pd
import io
import numpy as np
data_string = """Name Lang skills
Michael http 1
Cristiano css 2
John js 3
Piter http 4
Michael css 3
Cristiano js 2
John http 1
Piter css 4
NaN js 1
"""
df = pd.read_csv(io.StringIO(data_string), sep='\s ')
df['Name'].replace(np.nan, 'Other', inplace=True)
pivot = pd.pivot_table(df, values='skills', index='Name', columns='Lang', aggfunc='sum')
CodePudding user response:
You can try this solution:
pivot = pd.pivot_table(df, values= 'skills', dropna=False, index = 'Name', columns= 'Lang', aggfunc ='sum')