Skip to main content
 首页 » 编程设计

python之来自 csv 的 Pandas Dataframe 显示不正确

2025年05月04日13soundcode

我正在尝试将一个 csv 文件(25MB - 80000 行)导入到 pandas 数据框中,但它没有正确显示。列用“;”分隔在调用 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 上非常清晰。为什么不能很好地被 pandas 解释。

请您参考如下方法:

你应该使用 delimiter或 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. 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=';')