简体   繁体   English

来自CSV的Pandas Dataframe无法正确显示

[英]Pandas Dataframe from csv not displaying correctly

I'm trying to import a csv file (25MB - 80000 rows) into pandas dataframe but it's not displaying correctly. 我正在尝试将csv文件(25MB-80000行)导入pandas数据框,但显示不正确。 The columns are seperated with a ';' 列之间用';'分隔。 when calling df.columns . 调用df.columns

  Originated GeoZone;Booking ...                                                                                                                                                                                                                                                                                                                                                          
1  PARIS;PARIS;;MARKer;EQDff;...                                                                                                                                                                                                                                                                                                                                                                                              
2  PARIS;PARIS;;MARKer;EQDff;...                                                                                                                                                                                                                                                                                                                                                                                              
3  PARIS;PARIS;;MARKer;EQDff;...                                                                                                                                                                                                                                                                                                                                                                                             
4  PARIS;PARIS;;MARKer;EQDff;...  

The csv file is perfectly clear on Excel. 在Excel中,csv文件非常清晰。 Why is it not well interpreted by pandas. 为什么熊猫不能很好地解释它。

This problem might be caused by different separator in Excel, it depends usually on country you are from. 此问题可能是由于Excel中的分隔符不同而引起的,通常取决于您来自的国家/地区。

You can try this: 您可以尝试以下方法:

pd.read_csv("your file", sep = ";")

This should works. 这应该工作。

Theory: this is caused by different separators for csv files. 理论:这是由csv文件的不同分隔符引起的。 Python use comma by default, but some files might use something other the comma, for example: ";" Python默认情况下使用逗号,但是某些文件可能使用其他逗号,例如:“;” or "|". 或“ |”。

Solution: 解:

pd.read_csv('your file', sep = ';')

As @RafaelC pointed out, your default delimiter is ; 正如@RafaelC指出的那样,您的默认分隔符为; , so you have to specify it separately ,因此您必须单独指定

You should use delimiter or sep attribute in read_csv : 您应该在read_csv使用定界符sep属性:

sep : str, default ','

Delimiter to use. 要使用的定界符。 If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python's builtin sniffer tool, csv.Sniffer. 如果sep为None,则C引擎无法自动检测到分隔符,但Python解析引擎可以,这意味着将使用后者,并通过Python的内置嗅探器工具csv.Sniffer自动检测到分隔符。 In addition, separators longer than 1 character and different from '\\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. 此外,超过1个字符且与'\\ s +'不同的分隔符将被解释为正则表达式,并且还将强制使用Python解析引擎。 Note that regex delimiters are prone to ignoring quoted data. 注意,正则表达式定界符易于忽略引用的数据。 Regex example: '\\r\\t'. 正则表达式示例:“ \\ r \\ t”。

delimiter : str, default None

Alias for sep. 9月的别名。

df = pd.read_csv('waka.csv', sep=';')

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

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