来自csv的Pandas数据帧未正确显示

2024-09-23 14:35:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图导入一个csv文件(25MB-80000行)到pandas dataframe,但它没有正确显示。调用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;...  

csv文件在Excel上非常清晰。为什么熊猫没有很好地解释它呢。在


Tags: columns文件csvdataframepandasdfexcelmarker
3条回答

您应该在read_csv中使用delimiter或{}属性:

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. 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. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\r\t'.

delimiter : str, default None

Alias for sep.

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

这个问题可能是由Excel中不同的分隔符引起的,这通常取决于您来自哪个国家。在

你可以试试这个:

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

这应该行得通。在

理论:这是由csv文件的不同分隔符造成的。Python默认使用逗号,但有些文件可能使用逗号以外的内容,例如:“;”或“|”。在

解决方案:

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

正如@rafalc指出的,默认的分隔符是;,因此必须单独指定它

相关问题 更多 >