【问题标题】:I got error like NameError: name 'df_clean' is not defined我收到类似 NameError 的错误:未定义名称“df_clean”
【发布时间】:2021-09-23 18:32:46
【问题描述】:

统计手机制造商

top3 = df_clean.info()['Handset Manufacturer'].value_counts().head(3)

top3

【问题讨论】:

标签: python-3.5


【解决方案1】:

2个问题:一个问题是你需要定义df_clean变量应该指向什么数据,比如:

import pandas as pd

df_clean = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df_clean.info()

输出:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   A       3 non-null      int64
 1   B       3 non-null      int64
dtypes: int64(2)
memory usage: 176.0 bytes

另一个问题是pandas.DataFrame.info 方法返回None,所以你无法从df_clean.info() 下标(就像你在代码中所做的df_clean.info()['Handset Manufacturer']。尝试删除 .info() 部分,例如:

top3 = df_clean['A'].value_counts().head(3)
print(top3)

输出:

0    1
1    2
2    3
Name: A, dtype: int64

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多