英文:
Finding the index of an element is list in a Pandas Dataframe column
问题
我需要在 pandas 中执行以下操作:
import pandas as pd
# 创建示例数据帧
data = {'col_a': ['hello'],
'col_b': [['goodbye', 'how are you', 'hello']],
'col_c': [['farewell', 'question', 'greeting']]}
df = pd.DataFrame(data)
# 使用 apply 方法获取所需的值
df['col_d'] = df.apply(lambda row: row['col_c'][row['col_b'].index(row['col_a'][0])], axis=1)
# col_d 中包含所需的值
通过这种方式,你可以在 Pandas 中找到所需的值并将其放入一个新的列(在此示例中是 'col_d')。
英文:
I have a pandas dataframe:
col_a col_b col_c
hello [goodbye, how are you, hello] [farewell, question, greeting]
I need to find the value in the list in col_c which has the same index as the value of col_a in the list col_b. So in this case: "hello" has index 2 in [goodbye, how are you, hello]. I need to get the value with index 2 from col_c - in this case "greeting".
How can I do this in pandas?
答案1
得分: 2
df['out'] = [next((c2 for b2, c2 in zip(b, c) if a in b2), None)
for a, b, c in zip(df['col_a'], df['col_b'], df['col_c'])]
输出:
col_a col_b col_c out
0 hello [goodbye, how are you, hello] [farewell, question, greeting] greeting
英文:
You can use a list comprehension with zip
and next
:
df['out'] = [next((c2 for b2, c2 in zip(b, c) if a in b2), None)
for a, b, c in zip(df['col_a'], df['col_b'], df['col_c'])]
Output:
col_a col_b col_c out
0 hello [goodbye, how are you, hello] [farewell, question, greeting] greeting
答案2
得分: 2
已添加get_result_value(row)
函数并实现了逻辑,该函数会根据列b
中与列a
对应的索引获取结果,并返回列c
中的结果。
已附上结果。
data_set = {'col_a': ['hello'],
'col_b': [['goodbye', 'how are you', 'hello']],
'col_c': [['farewell', 'question', 'greeting']]}
df = pd.DataFrame(data_set)
def get_result_value(row):
index = row['col_b'].index(row['col_a'])
return row['col_c'][index]
print(df.apply(get_result_value, axis=1))
结果:
英文:
Just added the function for get_result_value(row)
and implemented logic as the index will contain column b
corresponding to column a
then will get a result from the index and column c
.
I have attached the result as well.
data_set = {'col_a': ['hello'],
'col_b': [['goodbye', 'how are you', 'hello']],
'col_c': [['farewell', 'question', 'greeting']]}
df = pd.DataFrame(data_set)
def get_result_value(row):
index = row['col_b'].index(row['col_a'])
return row['col_c'][index]
print(df.apply(get_result_value, axis=1))
Result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论