英文:
Adding a column to a dataframe based on another dataframe
问题
我有一个类似这样的数据框
some_info THIS_info
abd set_1
def set_1
www set_1
qqq set_2
wws set_2
2222 set_3
和另一个数据框如下
THIS_info this_algo
set_1 algo_1
set_2 algo_2
set_3 algo_2
我想在第一个数据框中添加一列,基于"THIS_info"的信息,以便我可以得到
some_info THIS_info this_algo
abd set_1 algo_1
def set_1 algo_1
www set_1 algo_1
qqq set_2 algo_2
wws set_2 algo_2
2222 set_3 algo_2
有办法实现这个吗?
英文:
I have a dataframe like this
some_info THIS_info
abd set_1
def set_1
www set_1
qqq set_2
wws set_2
2222 set_3
and another dataframe like this
THIS_info this_algo
set_1 algo_1
set_2 algo_2
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
some_info THIS_info this_algo
abd set_1 algo_1
def set_1 algo_1
www set_1 algo_1
qqq set_2 algo_2
wws set_2 algo_2
2222 set_3 algo_2
Is there a way to achieve this?
答案1
得分: 2
你可以使用 merge 函数来合并两个数据框(df1 和 df2):
df1 = pd.merge(df1, df2, how='left', on='THIS_info')
print(df1)
输出:
some_info THIS_info this_algo
abd set_1 algo_1
def set_1 algo_1
www set_1 algo_1
qqq set_2 algo_2
wws set_2 algo_2
2222 set_3 algo_2
请注意,在这个示例中,参数 how
和 on
是可选的,但在更一般的情况下很有用。
英文:
You can use the merge function to join the two dataframes (df1 and df2):
df1 = pd.merge(df1, df2, how='left', on='THIS_info')
print(df1)
Output:
some_info THIS_info this_algo
abd set_1 algo_1
def set_1 algo_1
www set_1 algo_1
qqq set_2 algo_2
wws set_2 algo_2
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
函数,例如
import pandas as pd
df_left = pd.DataFrame({
"some_info": ["abd", "def", "www", "qqq", "wws", "2222"],
"THIS_info": ["set_1", "set_1", "set_1", "set_2", "set_2", "set_3"]
})
df_right = pd.DataFrame({
"THIS_info": ["set_1", "set_2", "set_3"],
"this_algo": ["algo_1", "algo_2", "algo_2"]
})
df = df_left.merge(df_right, on="THIS_info", how="left")
print(df)
然后你可以获得输出:
some_info THIS_info this_algo
0 abd set_1 algo_1
1 def set_1 algo_1
2 www set_1 algo_1
3 qqq set_2 algo_2
4 wws set_2 algo_2
5 2222 set_3 algo_2
英文:
You can use merge
function, like
import pandas as pd
df_left = pd.DataFrame({
"some_info": ["abd", "def", "www", "qqq", "wws", "2222"],
"THIS_info": ["set_1", "set_1", "set_1", "set_2", "set_2", "set_3"]
})
df_right = pd.DataFrame({
"THIS_info": ["set_1", "set_2", "set_3"],
"this_algo": ["algo_1", "algo_2", "algo_2"]
})
df = df_left.merge(df_right, on="THIS_info", how="left")
print(df)
Then you can get output:
some_info THIS_info this_algo
0 abd set_1 algo_1
1 def set_1 algo_1
2 www set_1 algo_1
3 qqq set_2 algo_2
4 wws set_2 algo_2
5 2222 set_3 algo_2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论