我正在创建一个数据帧,然后将该数据帧转换为数据透视表。数据透视表中的文本和列标题与我的结果中心对齐。我想将文本设置为" left"。你能帮忙吗?我已尝试df.to_string(justify = 'true')
,但会引发属性错误"'Unicode' object has no attribute 'columns'"
这是我的数据框:
df = DataFrame({'Customer': CustomerCOL,'Title': titleCOL,'count':countCOL})
table = pivot_table(df,index = ['Customer','Title'],values='count')
答案 0 :(得分:0)
我认为您需要在to_string
中将参数justify
设置为left
:
import pandas as pd
df = pd.DataFrame({'Customer': ['Ann Green', 'Joseph Smith', 'Ann Green'],
'Title': ['Ms', 'Mr', 'Ms'],
'count':[4, 6, 7]})
print (df)
Customer Title count
0 Ann Green Ms 4
1 Joseph Smith Mr 6
2 Ann Green Ms 7
table = pd.pivot_table(df,index = ['Customer','Title'],values='count').reset_index()
print (table)
Customer Title count
0 Ann Green Ms 5.5
1 Joseph Smith Mr 6.0
print (table.to_string(justify = 'left'))
Customer Title count
0 Ann Green Ms 5.5
1 Joseph Smith Mr 6.0