【问题标题】:Using df.dropna() returns NoneType Object使用 df.dropna() 返回 NoneType 对象
【发布时间】:2019-10-20 12:04:41
【问题描述】:

我有一段代码如下所示:

import pandas as pd
file = pd.read_csv("/my/path/to/spreadsheet.csv", index_col=0)
file = file.dropna(how="any", inplace=True)
file = file.fillna("", inplace=False)
print(file)

预期输出:

       Profit ($)  Spendings ($)      Total Profit  EOY Profit ($)
Month                                                                       
Jan           200             80               120            3150
Feb           310             50               260                          
Mar           250             40               210                          
Apr           170             70               100                          
May           650            200               450                          
Jun           180            150                30                          
Jul           530            160               370                          
Aug           610            270               340                          
Sep           470            180               290                          
Oct           680            290               390                          
Nov           570            310               260                          
Dec           600            270               330                          

电流输出:

Traceback(最近一次调用最后一次): 文件“/my/path/to/OpenSheet.py”,第 5 行,在 file = file.fillna("", inplace=False) AttributeError: 'NoneType' 对象没有属性 'fillna'

我知道这意味着当我执行file = file.dropna(how="any", inplace=True) 时,它以某种方式变成了NoneType 对象,但这是为什么呢?

另外,谁能告诉我如何获得预期的输出?

【问题讨论】:

  • 阅读有关“inplace”参数的文档。

标签: python python-3.x pandas


【解决方案1】:

这是因为inplace=True 参数就地修改了您的参数,这意味着该函数返回None。改成

import pandas as pd
file = pd.read_csv("/my/path/to/spreadsheet.csv", index_col=0)
file.dropna(how="any", inplace=True)
file = file.fillna("", inplace=False)
print(file)

此外,您的 EOY Profit 列似乎会导致几乎所有行都被删除:

file.dropna(how="any", inplace=True)

file
  EOY_Profit Month Profit Spendings Total_Profit
0       3150   Jan    200        80          120

所以我会完全避免 dropna

file = pd.read_csv("/my/path/to/spreadsheet.csv", index_col=0)
file = file.fillna("", inplace=False)
    EOY_Profit Month Profit Spendings Total_Profit
0        3150   Jan    200        80          120
1               Feb    310        50          260
2               Mar    250        40          210
3               Apr    170        70          100
4               May    650       200          450
5               Jun    180       150           30
6               Jul    530       160          370
7               Aug    610       270          340
8               Sep    470       180          290
9               Oct    680       290          390
10              Nov    570       310          260
11              Dec    600       270          330

【讨论】:

    猜你喜欢
    • 2021-03-12
    • 2019-01-12
    • 2020-06-04
    • 2020-05-06
    • 2021-06-24
    • 1970-01-01
    • 2012-07-25
    • 2018-11-29
    • 1970-01-01
    相关资源
    最近更新 更多