python - 值错误: Cannot use name of an existing column for indicator column

标签 python pandas

我需要解决一个问题,我将有一个数据框,例如 df,其中包含姓名和年龄,并且我需要在 for 循环中生成另一个包含姓名和性别的数据框,并且我需要合并 for 中生成的数据框与 df 循环以获取 df 中的性别。所以我在解决我的问题之前尝试了下面的代码

import pandas as pd
d = {'Age': [45, 38], 'Name': ['John', 'Emily']}
df = pd.DataFrame(data=d)
d1={'Gender':['M'],'Name':['John']}
df1=pd.DataFrame(data=d1)

df3 = df.merge(df1, on=['Name'], how='left', indicator=True)
df3

d2={'Gender':['F'],'Name':['Emily']}
df4=pd.DataFrame(data=d2)
df5=df3.merge(df4, on=['Name'], how='left', indicator=True)

我在运行最后一行时遇到以下错误。

 "Cannot use name of an existing column for indicator column")

ValueError: Cannot use name of an existing column for indicator column

你能建议我如何在 python 3.x 中解决这个问题吗?

最佳答案

有更好的方法来完成您想要做的事情(正如另一个人回答的那样)。但要了解为什么会出现错误,请阅读以下内容。

因为您进行了一次合并,所以您的 df3.txt 文件中现在有一个名为 _merge 的列。当您再次合并时,您无法创建另一个 _merge

顺便说一句,供将来引用,现在您有 indicator=True 但您也可以传入一个字符串,例如 indicator='exists' 然后您的新“指示”您如何加入的列将被称为 exists,您可以通过执行 df5['exists']

来选择它

查看这个简单的示例并在 repl 中演练它

>>> df1
  col1 col2
0    a    b
1    b    c
2    d    e
>>> df2
  col1 col2
0    a    b
1    b    c
>>> df1.merge(df2, on='col1', how='left', indicator=True)
  col1 col2_x col2_y     _merge
0    a      b      b       both
1    b      c      c       both
2    d      e    NaN  left_only
>>> df3 = df1.merge(df2, on='col1', how='left', indicator=True)
>>> df4 = pd.DataFrame([['d', 'e']], columns=['col1', 'col2'])
>>> df3.merge(df4, on='col1', how='left', indicator=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-packages/pandas/core/frame.py", line 4722, in merge
copy=copy, indicator=indicator)
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-packages/pandas/core/reshape/merge.py", line 54, in merge
return op.get_result()
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-packages/pandas/core/reshape/merge.py", line 567, in get_result
self.left, self.right)
  File "/usr/local/devtools/uat/anaconda4321/lib/python3.6/site-packages/pandas/core/reshape/merge.py", line 605, in _indicator_pre_merge
"Cannot use name of an existing column for indicator column")
ValueError: Cannot use name of an existing column for indicator column
>>> df3.merge(df4, on='col1', how='left', indicator='exists')
  col1 col2_x col2_y     _merge col2     exists
0    a      b      b       both  NaN  left_only
1    b      c      c       both  NaN  left_only
2    d      e    NaN  left_only    e       both

关于python - 值错误: Cannot use name of an existing column for indicator column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48669316/

相关文章:

python - 当行具有不同顺序的相同值时删除行

python Pandas : How can I sum all of the values of a dictionary in a column of my dataframe?

pandas - 测试 Pandas 代码

python - 如何将字符串列与 Null 合并

python - 如何根据列中的条件进行计算?

python - 在 ubuntu 18.04 安装 spyder-vim

python - 从列表列表中快速选择某些索引的项目

python - linux crontab 不能运行带图形界面的程序?

python - 通过 PyQt 中 QTableView 和 QTableWidget 的上下文菜单传递实例

python - Pandas 正确列出列表中的值