2016-06-11 92 views
0

我正在创建一个数据帧,然后将该数据帧转换为数据透视表。数据透视表中的文本和列标题对齐,以便在我的结果中居中。我想将文字对齐设置为“左”。你能帮忙吗?我试过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

嗨乔,通过文本我的意思是在Excel单元格中的数据。例如:在我将数据帧转换为数据透视表之后。我正在将数据写回excel。在Excel方面,数据的理由或对齐是“中心”。我想将数据对齐到“左”。这有帮助吗?我试图附上一个样本数据与当前和预期的输出,但我没有看到这里的附件选项。 – user3063530

回答

0

我想你需要设置参数justifyleftto_string

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 
+0

嗨,感谢您的回复,左侧的“display.colheader_justify”仅适用于列标题而非实际数据。 – user3063530

+0

请参阅编辑。 – jezrael

+0

嘿,再次感谢,我试过,但遇到这个错误“如果len(to_filter) user3063530