在Pandas数据框列中查找元素的索引。

huangapple go评论101阅读模式
英文:

Finding the index of an element is list in a Pandas Dataframe column

问题

我需要在 pandas 中执行以下操作:

  1. import pandas as pd
  2. # 创建示例数据帧
  3. data = {'col_a': ['hello'],
  4. 'col_b': [['goodbye', 'how are you', 'hello']],
  5. 'col_c': [['farewell', 'question', 'greeting']]}
  6. df = pd.DataFrame(data)
  7. # 使用 apply 方法获取所需的值
  8. df['col_d'] = df.apply(lambda row: row['col_c'][row['col_b'].index(row['col_a'][0])], axis=1)
  9. # col_d 中包含所需的值

通过这种方式,你可以在 Pandas 中找到所需的值并将其放入一个新的列(在此示例中是 'col_d')。

英文:

I have a pandas dataframe:

  1. col_a col_b col_c
  2. 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

你可以使用列表推导式与 zipnext 来实现:

  1. df['out'] = [next((c2 for b2, c2 in zip(b, c) if a in b2), None)
  2. for a, b, c in zip(df['col_a'], df['col_b'], df['col_c'])]

输出:

  1. col_a col_b col_c out
  2. 0 hello [goodbye, how are you, hello] [farewell, question, greeting] greeting
英文:

You can use a list comprehension with zip and next:

  1. df['out'] = [next((c2 for b2, c2 in zip(b, c) if a in b2), None)
  2. for a, b, c in zip(df['col_a'], df['col_b'], df['col_c'])]

Output:

  1. col_a col_b col_c out
  2. 0 hello [goodbye, how are you, hello] [farewell, question, greeting] greeting

答案2

得分: 2

已添加get_result_value(row)函数并实现了逻辑,该函数会根据列b中与列a对应的索引获取结果,并返回列c中的结果。

已附上结果。

  1. data_set = {'col_a': ['hello'],
  2. 'col_b': [['goodbye', 'how are you', 'hello']],
  3. 'col_c': [['farewell', 'question', 'greeting']]}
  4. df = pd.DataFrame(data_set)
  5. def get_result_value(row):
  6. index = row['col_b'].index(row['col_a'])
  7. return row['col_c'][index]
  8. print(df.apply(get_result_value, axis=1))

结果:

在Pandas数据框列中查找元素的索引。

英文:

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.

  1. data_set = {'col_a': ['hello'],
  2. 'col_b': [['goodbye', 'how are you', 'hello']],
  3. 'col_c': [['farewell', 'question', 'greeting']]}
  4. df = pd.DataFrame(data_set)
  5. def get_result_value(row):
  6. index = row['col_b'].index(row['col_a'])
  7. return row['col_c'][index]
  8. print(df.apply(get_result_value, axis=1))

Result:

在Pandas数据框列中查找元素的索引。

huangapple
  • 本文由 发表于 2023年6月29日 01:09:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定