【问题标题】:How to separate variables in list: return alphanumeric or numeric values如何分隔列表中的变量:返回字母数字或数值
【发布时间】:2020-09-08 20:18:36
【问题描述】:

我从某个df中提取了列,对如何根据这篇文章标题进行操作有疑问。

df_example = pd.read_csv(r"C:\Users\example.csv")
df = df_example.columns
df_numerics_only = df.select_dtypes(include=np.number)
print (df_numerics_only)

并得到以下错误:

AttributeError: 'Index' 对象没有属性 'index'

【问题讨论】:

  • 能否提供example.csv的示例内容?
  • @Henfu 你能确认一下你在这里使用了哪些 SciPy 库吗?我已经用 pandas 和 numpy 标记了这个问题,以防万一。

标签: python database dataframe data-science data-cleaning


【解决方案1】:

您的第 2 行只是将列名存储在变量 df 中。 df 不是此处的数据框,因此 select_dtypes() 会引发错误。

只需删除第二行并在您的数据帧上调用 select_dtypes()。

df_example = pd.read_csv(r"C:\Users\example.csv")
df_numerics_only = df_example.select_dtypes(include=np.number)
print (df_numerics_only)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2022-06-14
    • 2016-11-02
    • 2014-12-17
    • 2017-03-13
    • 1970-01-01
    • 2020-10-09
    相关资源
    最近更新 更多