pandas.pivot_table是为了支持数据分析,帮助你创建类似excel的数据透视表,而不是读取excel数据透视表。
创建电子表格样式的数据透视表作为 DataFrame。数据透视表中的级别将存储在结果 DataFrame 的索引和列上的 MultiIndex 对象(分层索引)中
文档中的示例
>>> df
A B C D
0 foo one small 1
1 foo one large 2
2 foo one large 2
3 foo two small 3
4 foo two small 3
5 bar one large 4
6 bar one small 5
7 bar two small 6
8 bar two large 7
>>> table = pivot_table(df, values='D', index=['A', 'B'],
... columns=['C'], aggfunc=np.sum)
>>> table
small large
foo one 1 4
two 6 NaN
bar one 5 4
two 6 7
现在为了帮助您解决这个问题,我创建了一个示例数据集和一个数据透视表。
然后将 Excel 工作表读入 pandas 数据框。该数据帧包含要替换的 nandf.fillna(方法='ffill')
df = pd.read_excel(pviotfile,skiprows=12,header=0)
df=df.fillna(method='ffill')
print (df)
output
Sports Name Address Age
0 basketball Abhijit 129 ABC 20
1 basketball Rajesh 128 ABC 20
2 Cricket Mahesh 123 ABC 20
3 Cricket Ramesh 126 ABC 20
4 Cricket Suresh 124 ABC 20
5 Football Riyash 125 ABC 20
6 Football suraj 127 ABC 20