【问题标题】:Specify dtype option on import or set low_memory=False在导入时指定 dtype 选项或设置 low_memory=False
【发布时间】:2019-05-14 00:24:02
【问题描述】:

我正在使用以下代码:

df = pd.read_csv('/Python Test/AcquirerRussell3000.csv')

我有以下类型的数据:

18.07.2000  27.1875         0 08.08.2000  25.3125       0.1 05.09.2000  \
0   19.07.00  26.6250 -0.020690   09.08.00  25.2344 -0.003085   06.09.00   
1   20.07.00  26.6250  0.000000   10.08.00  25.1406 -0.003717   07.09.00   
2   21.07.00  25.6875 -0.035211   11.08.00  25.5781  0.017402   08.09.00   
3   24.07.00  26.2500  0.021898   14.08.00  25.4375 -0.005497   11.09.00   
4   25.07.00  26.6875  0.016667   15.08.00  25.5625  0.004914   12.09.00 

我收到以下错误:

Pythone Test/untitled0.py:1: DtypeWarning: Columns (long list of numbers) have mixed types. 
Specify dtype option on import or set low_memory=False.

所以每第三列是一个日期,其余的是数字。我想没有单一的 dtype,因为日期是字符串,其余的是浮点数或整数?我有大约 5000 列或更多列和大约 400 行。

我已经看到了与此类似的问题,但不太知道如何将其应用于我的数据。此外,我想在堆叠数据帧之后运行以下代码。

a = np.arange(len(df.columns))
df.columns = [a % 3, a // 3]
df = df.stack().reset_index(drop=True)

df.to_csv('AcquirerRussell3000stacked.csv', sep=',')

我应该使用什么数据类型?还是我应该将 low_memory 设置为 false?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    这解决了我来自here的问题

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

    谁能给我解释一下这个答案?

    【讨论】:

    • 看起来像 "dtype='unicode'" 做的事情
    猜你喜欢
    • 1970-01-01
    • 2014-08-06
    • 2022-12-08
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 2018-07-17
    相关资源
    最近更新 更多