I have a pandas dataframe as in the code. The output needed is like this. But it seems the aggfunc is not working in the pivot_table. Please refer to the code below. Any help is appreciated. Thanks.
dataframe:
x y z
0 1 Mn 6.78
1 1 na 7.4
2 2 al 32
expected output :
x mn na al
1 6.78 7.4 0
2 0 0 32
import pandas as pd
import numpy as np
data=[['1','Mn','6.78'],['1','na','7.4'],['2','al','32']]
df = pd.DataFrame(data,columns=['x','y','z'])
print(df)
print(df.pivot_table(index='x', columns='y', aggfunc=len,values='z'))
the output I am getting is (The values of column z are not being filled) :
y Mn al na
x
1 1.0 NaN 1.0
2 NaN 1.0 NaN