【问题标题】:Appending two dataframes - AttributeError: 'NoneType' object has no attribute 'is_extension'附加两个数据框 - AttributeError: 'NoneType' 对象没有属性 'is_extension'
【发布时间】:2020-01-10 15:38:15
【问题描述】:

我有 2 个数据框(df1 和 df2),它们看起来像:

df1

Quarter Body    Total requests  Requests Processed  …   Requests on-hold 
Q3 2019    A                93                  92  …                  0
Q3 2019    B               228                 210  …                  0
Q3 2019    C               180                 178  …                  0
Q3 2019    D                31                  31  …                  0
Q3 2019    E               555                 483  …                  0

df2

Quarter Body    Total requests  Requests Processed  …   Requests on-hold 
Q2 2019    A                50                  50  …                  0
Q2 2019    B               191                 177  …                  0
Q2 2019    C               186                 185  …                  0
Q2 2019    D                35                  35  …                  0
Q2 2019    E               344                 297  …                  0

我正在尝试将 df2 附加到 df2 以创建 df3:

df3

Quarter Body    Total requests  Requests Processed  …   Requests on-hold 
Q3 2019    A                93                  92  …                  0
Q3 2019    B               228                 210  …                  0
Q3 2019    C               180                 178  …                  0
Q3 2019    D                31                  31  …                  0
Q3 2019    E               555                 483  …                  0
Q2 2019    A                50                  50  …                  0
Q2 2019    B               191                 177  …                  0
Q2 2019    C               186                 185  …                  0
Q2 2019    D                35                  35  …                  0
Q2 2019    E               344                 297  …                  0

使用:

df3= df1.append(df2) 

但得到错误:

AttributeError: 'NoneType' object has no attribute 'is_extension'

完整的错误跟踪是:

File "<ipython-input-405-e3e0e047dbc0>", line 1, in <module>
    runfile('C:/2019_Q3/Code.py', wdir='C:/2019_Q3')

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/2019_Q3/Code.py", line 420, in <module>
    main()

  File "C:/2019_Q3/Code.py", line 319, in main
    df3= df1.append(df2, ignore_index=True)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\frame.py", line 6692, in append
    sort=sort)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\reshape\concat.py", line 229, in concat
    return op.get_result()

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\reshape\concat.py", line 426, in get_result
    copy=self.copy)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\internals\managers.py", line 2056, in concatenate_block_managers
    elif is_uniform_join_units(join_units):

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\internals\concat.py", line 379, in is_uniform_join_units
    all(not ju.is_na or ju.block.is_extension for ju in join_units) and

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\internals\concat.py", line 379, in <genexpr>
    all(not ju.is_na or ju.block.is_extension for ju in join_units) and

AttributeError: 'NoneType' object has no attribute 'is_extension'

使用:

df3= pd.concat([df1, df2], ignore_index=True)

给我一​​个错误:

InvalidIndexError: Reindexing only valid with uniquely valued Index objects

完整的错误跟踪是:

Traceback (most recent call last):

  File "<ipython-input-406-e3e0e047dbc0>", line 1, in <module>
    runfile('C:/2019_Q3/Code.py', wdir='C:/2019_Q3')

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/2019_Q3/Code.py", line 421, in <module>
    main()

  File "C:/2019_Q3/Code.py", line 321, in main
    finalCSV = pd.concat([PreviousCSVdf, df], ignore_index=True)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\reshape\concat.py", line 228, in concat
    copy=copy, sort=sort)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\reshape\concat.py", line 381, in __init__
    self.new_axes = self._get_new_axes()

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\reshape\concat.py", line 448, in _get_new_axes
    new_axes[i] = self._get_comb_axis(i)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\reshape\concat.py", line 469, in _get_comb_axis
    sort=self.sort)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\indexes\api.py", line 70, in _get_objs_combined_axis
    return _get_combined_index(obs_idxes, intersect=intersect, sort=sort)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\indexes\api.py", line 117, in _get_combined_index
    index = _union_indexes(indexes, sort=sort)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\indexes\api.py", line 183, in _union_indexes
    result = result.union(other)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\indexes\base.py", line 2332, in union
    indexer = self.get_indexer(other)

  File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\pandas\core\indexes\base.py", line 2740, in get_indexer
    raise InvalidIndexError('Reindexing only valid with uniquely'

df1df2 具有相同的列数和列名。我将如何附加 df1 和 df2?

【问题讨论】:

  • 熊猫版?看起来像一个错误,卸载并重新安装熊猫。
  • 您能检查数据集中的重复列名吗?
  • 您在某处或某处有错字。我猜df2 根本不是数据框,而是None。否则appendconcat 都应该工作。
  • 感谢 piRSquared。如何将 df2 更改为数据框?
  • 我的猜测不可能是正确的。 df2 = None; df1.append(df2) 确实有效。嗯。

标签: python pandas


【解决方案1】:

当您在一个或两个数据集中有重复的列时,往往会发生这种情况。

此外,对于一般用途,使用pd.concat 更容易:

pd.concat([df1, df2], ignore_index=True) # ignore_index will reset index for you

对于 InvalidIndexError,您可以删除重复的行:

df1 = df1.loc[~df1.index.duplicated(keep='first')]
df2 = df2.loc[~df2.index.duplicated(keep='first')]

【讨论】:

  • 谢谢 zipa - 以上给了我一个 InvalidIndexError: Reindexing only valid with uniquely valueed Index objects 错误。我已经编辑了问题的正文以包含完整的跟踪
  • @Stacey 我也添加了我对该错误的建议
【解决方案2】:

我会让这个简短而甜蜜。我遇到了同样的问题。

问题不是由重复的列名引起的,而是由不同数据类型的重复列名引起的。

如果您不先处理数据类型,则切换到 pd.concat 不会为您解决此问题。

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 2018-12-09
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    相关资源
    最近更新 更多