在另一个数据框基础上添加一列

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

Adding a column to a dataframe based on another dataframe

问题

我有一个类似这样的数据框

  1. some_info THIS_info
  2. abd set_1
  3. def set_1
  4. www set_1
  5. qqq set_2
  6. wws set_2
  7. 2222 set_3

和另一个数据框如下

  1. THIS_info this_algo
  2. set_1 algo_1
  3. set_2 algo_2
  4. set_3 algo_2

我想在第一个数据框中添加一列,基于"THIS_info"的信息,以便我可以得到

  1. some_info THIS_info this_algo
  2. abd set_1 algo_1
  3. def set_1 algo_1
  4. www set_1 algo_1
  5. qqq set_2 algo_2
  6. wws set_2 algo_2
  7. 2222 set_3 algo_2

有办法实现这个吗?

英文:

I have a dataframe like this

  1. some_info THIS_info
  2. abd set_1
  3. def set_1
  4. www set_1
  5. qqq set_2
  6. wws set_2
  7. 2222 set_3

and another dataframe like this

  1. THIS_info this_algo
  2. set_1 algo_1
  3. set_2 algo_2
  4. set_3 algo_2

I want to add a column to the first dataframe, based on the info on "THIS_info" so that I can get

  1. some_info THIS_info this_algo
  2. abd set_1 algo_1
  3. def set_1 algo_1
  4. www set_1 algo_1
  5. qqq set_2 algo_2
  6. wws set_2 algo_2
  7. 2222 set_3 algo_2

Is there a way to achieve this?

答案1

得分: 2

你可以使用 merge 函数来合并两个数据框(df1 和 df2):

  1. df1 = pd.merge(df1, df2, how='left', on='THIS_info')
  2. print(df1)

输出:

  1. some_info THIS_info this_algo
  2. abd set_1 algo_1
  3. def set_1 algo_1
  4. www set_1 algo_1
  5. qqq set_2 algo_2
  6. wws set_2 algo_2
  7. 2222 set_3 algo_2

请注意,在这个示例中,参数 howon 是可选的,但在更一般的情况下很有用。

英文:

You can use the merge function to join the two dataframes (df1 and df2):

  1. df1 = pd.merge(df1, df2, how='left', on='THIS_info')
  2. print(df1)

Output:

  1. some_info THIS_info this_algo
  2. abd set_1 algo_1
  3. def set_1 algo_1
  4. www set_1 algo_1
  5. qqq set_2 algo_2
  6. wws set_2 algo_2
  7. 2222 set_3 algo_2

Note that parameters how and on are optional in this example, but are useful in a more general situation.

答案2

得分: 1

你可以使用merge函数,例如

  1. import pandas as pd
  2. df_left = pd.DataFrame({
  3. "some_info": ["abd", "def", "www", "qqq", "wws", "2222"],
  4. "THIS_info": ["set_1", "set_1", "set_1", "set_2", "set_2", "set_3"]
  5. })
  6. df_right = pd.DataFrame({
  7. "THIS_info": ["set_1", "set_2", "set_3"],
  8. "this_algo": ["algo_1", "algo_2", "algo_2"]
  9. })
  10. df = df_left.merge(df_right, on="THIS_info", how="left")
  11. print(df)

然后你可以获得输出:

  1. some_info THIS_info this_algo
  2. 0 abd set_1 algo_1
  3. 1 def set_1 algo_1
  4. 2 www set_1 algo_1
  5. 3 qqq set_2 algo_2
  6. 4 wws set_2 algo_2
  7. 5 2222 set_3 algo_2
英文:

You can use merge function, like

  1. import pandas as pd
  2. df_left = pd.DataFrame({
  3. "some_info": ["abd", "def", "www", "qqq", "wws", "2222"],
  4. "THIS_info": ["set_1", "set_1", "set_1", "set_2", "set_2", "set_3"]
  5. })
  6. df_right = pd.DataFrame({
  7. "THIS_info": ["set_1", "set_2", "set_3"],
  8. "this_algo": ["algo_1", "algo_2", "algo_2"]
  9. })
  10. df = df_left.merge(df_right, on="THIS_info", how="left")
  11. print(df)

Then you can get output:

  1. some_info THIS_info this_algo
  2. 0 abd set_1 algo_1
  3. 1 def set_1 algo_1
  4. 2 www set_1 algo_1
  5. 3 qqq set_2 algo_2
  6. 4 wws set_2 algo_2
  7. 5 2222 set_3 algo_2

huangapple
  • 本文由 发表于 2023年3月1日 09:16:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598774.html
匿名

发表评论

匿名网友

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

确定