简体   繁体   English

如何读取 pandas 中缺少分隔符的 csv(或 - 带有其他分隔符)

[英]How to read a csv in pandas with a missing delimiter (or - with additional delimiters)

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=";")

This data looks as:该数据如下所示:

   one  two  three
1    2    3    NaN
4    5    6    NaN

The desired result is:期望的结果是:

   one  two  three
    1    2    3    
    4    5    6    

I could manually edit the csv, but I don't really want to have to do that if possible.可以手动编辑 csv,但如果可能的话,我真的不想这样做。

In R the function read_delim can manage this with something along the lines of read_delim( <path>, ";", escape_double = FALSE, trim_ws = TRUE)R中,function read_delim可以通过read_delim( <path>, ";", escape_double = FALSE, trim_ws = TRUE)

For me working index_col=False parameter:对我来说工作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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM