如何在缺少分隔符的熊猫中读取csv(或-附加分隔符)

时间:2020-07-20 12:06:30

标签: python pandas csv data-cleaning

import pandas as pd
from io import StringIO

csv_one = """\
one;two;three
1;2;3;
4;5;6;
"""
df1 = pd.read_csv(StringIO(csv), sep=";")

此数据如下:

   one  two  three
1    2    3    NaN
4    5    6    NaN

所需的结果是:

   one  two  three
    1    2    3    
    4    5    6    

可以手动编辑csv,但我真的不希望在可能的情况下这样做。

R中,功能read_delim可以用类似read_delim( <path>, ";", escape_double = FALSE, trim_ws = TRUE)的方式来管理它

1 个答案:

答案 0 :(得分:1)

对我来说,使用index_col=False参数:

csv = """\
one;two;three
1;2;3;
4;5;6;
"""
df1 = pd.read_csv(StringIO(csv), sep=";", index_col=False)
print (df1)
   one  two  three
0    1    2      3
1    4    5      6