Python Pandas-根据单元格值查找列值 [英] Python Pandas - Lookup a column value based on a cell value
问题描述
我正在尝试使用一个单元格的值在另一列中查找一个单元格的值.第一个单元格值指示要查找的列.
I'm trying to use the value of one cell to find the value of a cell in another column. The first cell value dictates which column to lookup.
import pandas as pd
df = pd.DataFrame({'A': ['John', 'Andrew', 'Bob', 'Fred'], 'B': [
'Fred', 'Simon', 'Andrew', 'Andrew'], 'source': ['A', 'B', 'A', 'B']}, )
print(df)
A B source
0 John Fred A
1 Andrew Simon B
2 Bob Andrew A
3 Fred Andrew B
我在输出"列中所需的输出值是对源"的查找
My required output value in the 'output' column is a lookup of the 'source'
A B source output
0 John Fred A John
1 Andrew Simon B Simon
2 Bob Andrew A Bob
3 Fred Andrew B Andrew
尝试失败
df['output'] = df[df['source']]
这将导致 ValueError:错误传递了4个项目,放置的含义为1 ,因为 df ['source']
是通过Series而不是字符串传递的.我尝试使用以下方法转换为字符串:
This results in a ValueError: Wrong number of items passed 4, placement implies 1 because the df['source']
passes in a Series, not a string. I tried converting to a string using:
df['output'] = df[df['source'].convertDTypes(convert_string=True)]
给出错误 AttributeError:系列"对象没有属性"convertDTypes" .
工作解决方案
我发现一种解决方案可能是使用以下方法来遍历行:
I found a solution might by iterating through the rows using:
for index, row in df.iterrows():
column = df.loc[index, 'source']
df.at[index, 'output'] = df.loc[index, column]
但是,这篇文章提示,迭代是一个坏主意.代码也不是很优雅.
However, this post suggests iterating is a bad idea. The code doesn't seem very elegant, either.
我觉得我在这里错过了一些基本的东西;这真的不应该那么难.
I feel I've missed something basic here; this really should not be that hard.
推荐答案
让我们以 numpy
的方式进行操作,因为 lookup
在以后的版本中将不再起作用
Let us do numpy
way since lookup
will not longer work in the future version
df['new'] = df.values[df.index,df.columns.get_indexer(df.source)]
df
Out[339]:
A B source new
0 John Fred A John
1 Andrew Simon B Simon
2 Bob Andrew A Bob
3 Fred Andrew B Andrew
这篇关于Python Pandas-根据单元格值查找列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!