Python CSV Pandas DataFrame.to_string()未读取完整列值

时间:2017-09-11 07:47:35

标签: python pandas csv dataframe tostring

我正在使用Pandas v0.20.1,尝试导入CSV文件并将少数列转换为字符串。我的CSV文件列大约有200多个字符,我想将这些列转换为字符串并打印。我怀疑函数df['Content'].to_string(header=False,index=False)是罪魁祸首,因为导入按预期发生,读取每一行。我将举一个大栏目的例子。

example.csv

ID, Content
1, I am the big column with more than 50 characters to produce an example for question

代码:

import pandas as pd

dataFrame = pd.read_csv('example.csv')
print("Before modifying pd.options.display.max_colwidth")
print(dataFrame)
string1 = df['Content'].to_string(header=False,index=False)
print(string1)

print("After modifying pd.options.display.max_colwidth to some big value")
pd.options.display.max_colwidth = 500
print(dataFrame)
print(string1)
string2 = df['Content'].to_string(header=False,index=False)
print(string2)

输出:

Before modifying pd.options.display.max_colwidth
   ID                                            Content
0   1  I am the big column with more than 50 characte...
I am the big column with more than 50 characte...

After modifying pd.options.display.max_colwidth to some big value
   ID                                            Content
0   1 I am the big column with more than 50 characters to produce an example for question  
I am the big column with more than 50 characte...
I am the big column with more than 50 characters to produce an example for question

即使更改了max_colwidth值,string1也会相同。 我真正担心的是to_string(),它的行为取决于pd.options.display.max_colwidth值?或DataFrame方面的其他内容?

如果to_string()的任何其他替代方法会返回完整的字符串值吗?

0 个答案:

没有答案