我正在尝试标记一行,如果它们存在于数据框中。以下是我的代码片段:
MissingAATracking = []
for row in UK.UK:
if row in links_adobe_uk:
MissingAATracking.append("NO")
else:
MissingAATracking.append("YES")
我收到以下错误:
> --------------------------------------------------------------------------- TypeError Traceback (most recent call
> last) <ipython-input-94-5b85f0a628ec> in <module>()
> 1 for row in UK.UK:
> ----> 2 if row in links_adobe_uk:
> 3 MissingAATracking.append("NO")
> 4 else:
> 5 MissingAATracking.append("YES")
>
> /anaconda/lib/python3.6/site-packages/pandas/core/generic.py in
> __contains__(self, key)
> 905 def __contains__(self, key):
> 906 """True if the key is in the info axis"""
> --> 907 return key in self._info_axis
> 908
> 909 @property
>
> /anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py in
> __contains__(self, key) 1588 @Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
> 1589 def __contains__(self, key):
> -> 1590 hash(key) 1591 try: 1592 return key in self._engine
>
> TypeError: unhashable type: 'list'
我看过这里:Python, TypeError: unhashable type: 'list'
更新
以下是我的英国数据框架的主管:
UK
0 Link1
1 Link1
2 Link1
3 Link1
4 Link1
和我的links_adobe_uk:
PageURL
0 (null)
1 Link1
2 Link1
3 Link1
4 Link1
我还试图从两列中删除重复项,但我的英国数据框会抛出一个
TypeError:不可用类型:'list'
但我似乎无法跟随,因为UK和links_adobe_uk都是数据帧。我将非常感谢您的指导
答案 0 :(得分:1)
import pandas as pd
df1 = pd.DataFrame(data = {'col1' : [1, 2, 3, 4, 5]})
df2 = pd.DataFrame(data = {'col1' : [1, 2, 3]})
# merge both dataframes on the common column
common = df1.merge(df2,on=['col1'])
# wherever the common column is present in df1 return "YES" else "NO"
missing_tracking = ["Yes" if val else "No" for val in (df1.col1.isin(common.col1)).values ]
输出:
['Yes', 'Yes', 'Yes', 'No', 'No']
答案 1 :(得分:1)
由于数据框'links_adobe_uk'只包含一列,因此将其视为系列。这就是它显示错误“Unhashable type:list”的原因。因此,将系列转换为列表将解决问题
list_to_search = links_adobe_UK.PageURL.values.tolist()
MissingAATracking = []
for row in UK.UK:
if row in list_to_search:
MissingAATracking.append('No')
else:
MissingAATracking.append('Yes')
print(MissingAATracking)
答案 2 :(得分:0)
该计划的目标可分为两部分:
我认为与for row in UK.UK:
并非如此直接。
关于上述两个部分,请检查这两个答案
How to iterate over rows in a DataFrame in Pandas?
Pandas: Check if row exists with certain values