【问题标题】:Debug: Dataframe Column Referencing and Indexing [duplicate]调试:数据框列引用和索引 [重复]
【发布时间】:2019-06-15 06:19:12
【问题描述】:

我无法解决这个错误。我认为这是我对数据框和索引的误解。另外,可能是对 for 循环的误解。 (我习惯于 matlab for 循环......迭代,直观地说,更容易:D)

这是错误:

KeyError: "['United States' 'Canada' 'Mexico'] not found in axis"

这发生在线路:as_df=as_df.drop(as_df[column])

但这没有任何意义...我调用的是单个列而不是整个虚拟变量集。

以下代码可以复制运行。我确定了。

我的密码:

import pandas as pd
import numpy as np
df=pd.DataFrame({"country": ['United States','Canada','Mexico'], "price": [23,32,21], "points": [3,4,4.5]})
df=df[['country','price','points']]
df2=df[['country']]
features=df2.columns
print(features)
target='points'

#------_-__-___---____________________
as_df=pd.concat([df[features],df[target]],axis=1)
#Now for Column Check
for column in as_df[features]:
    col=as_df[[column]]
    #Categorical Data Conversion
#This will split the countries into their own column with 1 being when it 
#is true and 0 being when it is false
    col.select_dtypes(include='object')
    dummies=pd.get_dummies(col)
    #ML Check:
    dumcols=dummies.drop(dummies.columns[1],axis=1)
    if dumcols.shape[1] > 1:
        print(column)
        as_df=as_df.drop(as_df[column])
    else:
        dummydf=col
as_df=pd.concat([as_df,dummydf],axis=1)
as_df.head()

【问题讨论】:

    标签: python dataframe for-loop indexing


    【解决方案1】:

    发现我的错误。

    as_df=as_df.drop(as_df[column])
    

    应该是

    as_df=as_df.drop(column,axis=1)
    

    【讨论】:

      【解决方案2】:

      我会发表评论而不是回答,但我没有足够的声誉这样做。 (我需要澄清以帮助您,而 Stack Exchange 并没有为我提供“正确”这样做的方法。)

      我不完全确定您的最终目标是什么。您能否澄清 as_df 的最终结果会是什么样子?包括for循环结束之后,以及整个代码运行完之后?

      【讨论】:

      • 欢迎来到 SO。我也是新人,但有时这是一种祝福。我发现了我的错误。感谢您的尝试,我为我的模棱两可道歉。
      猜你喜欢
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2020-04-16
      • 1970-01-01
      相关资源
      最近更新 更多