【问题标题】:AttributeError: 'DataFrame' object has no attribute 'ix'AttributeError:“DataFrame”对象没有属性“ix”
【发布时间】:2020-05-16 09:15:33
【问题描述】:

当我尝试使用 pandas 数据框的 .ix 属性拉出一列时出现此错误,例如df.ix[:, 'col_header'].

AttributeError: 'DataFrame' object has no attribute 'ix'

该脚本今天早上可以运行,但今天下午我在全新安装了 Pandas 的新 Linux 环境中运行它。有没有其他人见过这个错误?我已经在这里和其他地方搜索过,但找不到。

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

试试df.iloc[:, integer]

.ix 已弃用

顺便说一句,df.loc[:,'col_header'] 用于 str 或布尔索引

【讨论】:

  • 我收到“基于位置的索引只能有 [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] 类型”。
  • "顺便说一句,df.loc[:,'col_header'] 用于 str 索引",如果索引器是布尔掩码,则需要。
  • ".ix is deprecated" - .ix 已被删除,而不是被弃用(这意味着不鼓励但仍然可用)。
【解决方案2】:

将 .ix 更改为 .loc,它应该可以正常工作。

【讨论】:

    【解决方案3】:

    今天(2020 年 1 月 30 日)全新安装将安装 pd.__version__ == '1.0.0'。随之而来的是removal of many deprecated features

    删除 Series.ix 和 DataFrame.ix (GH26438)

    【讨论】:

      【解决方案4】:

      尝试以下步骤: 1)安装新版本的熊猫 2) 使用 .loc 代替 .ix

      【讨论】:

        【解决方案5】:

        pandas 1.0.0 也有同样的问题,这对我有用

        以管理员身份打开 Anaconda Prompt (cmd),然后

        conda install pandas==0.25.1

        您的新熊猫版本将被旧版本覆盖!

        【讨论】:

          【解决方案6】:

          对我有用

          使用 df.loc[] 代替 ix[]

          【讨论】:

            【解决方案7】:

            我使用 .loc() 而不是 .ix() 并且它有效。

            【讨论】:

              【解决方案8】:

              用 .iloc 替换 .ix 后替换它对我也很有效

              predictions_ARIMA_log = pd.Series(ts_log.iloc[0], index=ts_log.index)

              【讨论】:

                【解决方案9】:

                一栏:

                df[['sepal width']]
                

                两列:

                df[['sepal width','petal width']]
                

                特殊列(选择列包括“长度”):

                df[[c for c in df.columns if 'length' in c]]
                

                【讨论】:

                  【解决方案10】:

                  我正在阅读 Wes McKinney'Python for data analysis'一书,在检索具有索引的行时遇到了 Dataframe.ix[] 的相同问题。 我用 iloc 替换了 ix,它工作得很好。

                  【讨论】:

                    【解决方案11】:

                    我使用的是 .ix,因为我混合了索引、标签和整数。 .loc() 不能像 .iloc 那样解决问题;两者都以错误结尾。我故意使用 .ix,因为当索引是整数和标签的混合时,它是快车道。

                    例如一个像这样的df:

                    我的出路是备份列和索引,用整数替换,使用 .iat 然后将 df 恢复为开始时的样子。我有类似的东西:

                    # Save the df and replace indec and columns with integers
                    lista_colonne = list(df.columns)  
                    df.columns = range(0,len(lista_colonne))    
                    nome_indice = df.index.name
                    lista_indice = list(df.index)
                    df['Indice'] = range(0,len(lista_indice))
                    df.index = df['Indice']
                    del df['Indice']
                    
                      ... indexing here with .iat in place of .ix
                    
                    
                    # Now back as it was
                    df.columns = lista_colonne
                    df['Indice'] = lista_indice
                    df.index = df['Indice']
                    del df['Indice']
                    df.index.name = nome_indice
                    

                    再见,法比奥。

                    【讨论】:

                      【解决方案12】:

                      我必须这样做:

                      returns.ix['2015-01-01':'2015-12-31'].std()
                      

                      经过一番折腾,我用这个实现了它:

                      returns.xs(key='2015',axis=0).std()
                      

                      我相信至少在这种情况下,我们可以使用横截面和过滤器,使用 2015 作为键。

                      【讨论】:

                        【解决方案13】:

                        是的,没错。将df.ix[] 替换为df.iloc[]df.loc[]

                        【讨论】:

                          【解决方案14】:
                          1. 大家尝试更新当前的熊猫
                          2. 将 .ix 替换为 .iloc 在更换它对我来说很好之后 有关详细信息,请参阅文档

                          【讨论】: