df = pd.read_csv('somefile.csv')

...给出一个错误:

熊猫…/网站/ / io / parsers.py: 1130: DtypeWarning:列(4,5,7,16)为混合类型。指定dtype 选项导入或设置low_memory=False。

为什么dtype选项与low_memory相关,为什么low_memory=False帮助?


当前回答

有时候,当其他方法都失败时,你只想告诉熊猫闭嘴:

# Ignore DtypeWarnings from pandas' read_csv                                                                                                                                                                                            
warnings.filterwarnings('ignore', message="^Columns.*")

其他回答

Try:

dashboard_df = pd.read_csv(p_file, sep=',', error_bad_lines=False, index_col=False, dtype='unicode')

根据熊猫的文件:

dtype:列的类型名称或字典->类型

至于low_memory,默认为True,还没有文档。但我认为这无关紧要。错误消息是通用的,所以无论如何您都不需要处理low_memory。希望这对你有所帮助,如果你还有其他问题,请告诉我

df = pd.read_csv('somefile.csv', low_memory=False)

这应该能解决问题。当从CSV中读取1.8M行时,我得到了完全相同的错误。

有时候,当其他方法都失败时,你只想告诉熊猫闭嘴:

# Ignore DtypeWarnings from pandas' read_csv                                                                                                                                                                                            
warnings.filterwarnings('ignore', message="^Columns.*")

正如错误所示,在使用read_csv()方法时应该指定数据类型。 所以,你应该写

file = pd.read_csv('example.csv', dtype='unicode')

这对我很管用!

file = pd.read_csv('example.csv', engine='python')