2
將數據透視表寫入csv文件時,是否可以編寫自己的列名?如何使用熊貓將用戶定義的列名寫入csv文件?
import pandas as pd
c1 = ['a','a','a','a','b','b','b','b']
c2 = ['x','x','y','y','x','x','y','y']
c3 = [1, 2, 1, 2, 1, 2, 1, 2]
val = [85, 47, 29, 93, 15, 21, 65, 16]
df = pd.DataFrame({'c1':c1, 'c2':c2, 'c3':c3, 'val':val})
ptable = pd.pivot_table(data=df, cols=['c2','c3'], rows='c1')
我試圖用header
參數:
ptable.to_csv('test.csv', header=['n1','n2','n3','n4'])
但列名沒有改變...