如何在csv中修复我的Pandas Dataframe输出

时间:2017-06-23 23:07:02

标签: python csv pandas dataframe

尝试运行以下代码时,我没有得到预期的结果。我希望输出跨越多列。

它看起来像是在这样的数据周围抛出引号 " column1,column2,column3"
" SDF,E,SD"

import pandas as pd
outputPath = r'C:\Users\Jfairfield\Desktop\output.csv'
testPath = r'C:\Users\Jfairfield\Desktop\test.csv'
csvData = pd.read_csv(testPath, 'Sheet1')
csvData.to_csv(outputPath, index=False)

输入:

enter image description here

输入文字
column1,column2,column3
SDF,E,SD

当前输出:

enter image description here

2 个答案:

答案 0 :(得分:3)

outputPath = r'C:\Users\xxx\Desktop\Python fo excel\mycsv - Copy.csv'
testPath = r'C:\Users\xxx\Desktop\Python fo excel\mycsv.csv'
csvData = pd.read_csv(testPath,  sep=',', engine='python')
print(csvData)
csvData.to_csv(outputPath, index=False, sep=',')

输出

     A  B   C
0  sdf  e  sd

答案 1 :(得分:1)

您可能在文本文件中有引号行,在读取数据时会转义分隔符,您可以尝试设置quoting=3(引用无)以避免此行为:

实施例

stripQuote = lambda x: x.strip('"')
​
df = pd.read_csv(StringIO("""
"a,b,c"
"d,e,f"
"""), quoting=3, converters={0: stripQuote, 2: stripQuote})
​
df.columns = ['a','b','c']

df
#   a   b   c
#0  d   e   f