【发布时间】:2019-07-08 12:54:31
【问题描述】:
我正在尝试连接两个沿 0 轴具有不同列名的数据框。我在这里How to use join_axes in the column-wise axis concatenation using pandas DataFrame? 发现了一个类似的问题,但是这个解决方案对我不起作用,因为我的两个数据框的列名不一样。由于我的原始数据太大而无法在此处发布,因此以下示例应说明我正在尝试做的事情:
df1 = pd.DataFrame(np.random.randint(0,100,size=(1, 4)), columns=list('ABCD'))
df2 = pd.DataFrame(np.random.randint(0,100,size=(1, 4)), columns=list('EFGH'))
#df1
A B C D
0 26 39 7 44
#df2
E F G H
0 12 44 26 64
pd.concat([df1,df2],axis=0).reset_index(drop=True)
# desired output looks like this
A B C D E F G H
0 26.0 39.0 7.0 44.0 NaN NaN NaN NaN
1 NaN NaN NaN NaN 12.0 44.0 26.0 64.0
上面的代码完美运行。但是,一旦我使用与上述完全相同的语法为 df1 和 df2 输入自己的数据帧,就会出现错误。
# my real dfs are called data1 & data2, I tried setting ignore_index=True and ignore_index=False
pd.concat([data1, data2],axis=0, ignore_index=True)
导致以下错误:
错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-194-dbee1fd0bdea> in <module>
----> 1 pd.concat([data1, data2],axis=0, ignore_index=True)
~\AppData\Local\Continuum\anaconda3\envs\tensorflow-gpu\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, sort, copy)
224 verify_integrity=verify_integrity,
225 copy=copy, sort=sort)
--> 226 return op.get_result()
227
228
~\AppData\Local\Continuum\anaconda3\envs\tensorflow-gpu\lib\site-packages\pandas\core\reshape\concat.py in get_result(self)
421 new_data = concatenate_block_managers(
422 mgrs_indexers, self.new_axes, concat_axis=self.axis,
--> 423 copy=self.copy)
424 if not self.copy:
425 new_data._consolidate_inplace()
~\AppData\Local\Continuum\anaconda3\envs\tensorflow-gpu\lib\site-packages\pandas\core\internals.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
5414 values = values.view()
5415 b = b.make_block_same_class(values, placement=placement)
-> 5416 elif is_uniform_join_units(join_units):
5417 b = join_units[0].block.concat_same_type(
5418 [ju.block for ju in join_units], placement=placement)
~\AppData\Local\Continuum\anaconda3\envs\tensorflow-gpu\lib\site-packages\pandas\core\internals.py in is_uniform_join_units(join_units)
5438 # no blocks that would get missing values (can lead to type upcasts)
5439 # unless we're an extension dtype.
-> 5440 all(not ju.is_na or ju.block.is_extension for ju in join_units) and
5441 # no blocks with indexers (as then the dimensions do not fit)
5442 all(not ju.indexers for ju in join_units) and
~\AppData\Local\Continuum\anaconda3\envs\tensorflow-gpu\lib\site-packages\pandas\core\internals.py in <genexpr>(.0)
5438 # no blocks that would get missing values (can lead to type upcasts)
5439 # unless we're an extension dtype.
-> 5440 all(not ju.is_na or ju.block.is_extension for ju in join_units) and
5441 # no blocks with indexers (as then the dimensions do not fit)
5442 all(not ju.indexers for ju in join_units) and
AttributeError: 'NoneType' object has no attribute 'is_extension'
我不太明白这条错误消息试图告诉我什么。我一直在尝试在两个数据帧上使用 fillna,这样就不应该再有“NoneType”了:
data2 = data2.fillna(999)
data1 = data1.fillna(999)
但是,我仍然收到相同的错误消息。
我使用的两个数据框非常大,所以很遗憾我不能在这里发布整个示例。我的两个数据框的内容只是整数、浮点数和字符串,所以这里没有什么特别的事情会引起可能的错误。关于可能导致此错误的原因或我可以检查以缩小问题范围的任何想法?
非常感谢!
【问题讨论】:
-
你能解释一下
is_extension是什么吗?这是列名吗? -
嗨@Efran,这不是我写的。从错误消息输出来看,它似乎在 pandas 核心内部(“~\AppData\Local\Continuum\anaconda3\envs\tensorflow-gpu\lib\site-packages\pandas\core\internals.py”)。
-
你的熊猫版本是什么?它在熊猫 0.22.0 中对我有用。
-
我的版本是'0.23.4'。抱歉,如果我造成了一些混乱:我上面的小代码示例也适用于我,但是问题是一旦我使用我的真实数据它就会中断。因此,我想它一定与我的数据有关。我希望错误消息能说明我的数据可能有什么问题......但我无法解释它。