简体   繁体   English

使用一个 Pandas 数据框填充另一个 Pandas 数据框的新列

[英]Using one pandas dataframe to populate new column in another pandas dataframe

I have two dataframes.我有两个数据框。 The first dataframe is df_states and the second dataframe is state_lookup .第一个数据帧是df_states ,第二个数据帧是state_lookup

df_states

   state         code     score
0  Texas         0        0.753549
1  Pennsylvania  0        0.998119
2  California    1        0.125751
3  Texas         2        0.125751
state_lookup

   state         code_0    code_1   code_2
0  Texas         2014      2015     2019
1  Pennsylvania  2015      2016     207
2  California    2014      2015     2019

I want to create a new column in df_states called 'year' which is based off the 'code' column which is based off the state_lookup table.我想在df_states创建一个名为“year”的新列,它基于基于state_lookup表的“code”列。 So for example, if Texas has a code = 0 then based off the state_lookup df the year should be 2014. If Texas has a code = 2, then the year should be 2019.例如,如果德克萨斯州的代码 = 0,那么根据state_lookup df,年份应该是 2014。如果德克萨斯州的代码 = 2,那么年份应该是 2019。

This is what the end result should look like:最终结果应该是这样的:

df_states

   state         code     score      year
0  Texas         0        0.753      2014
1  Pennsylvania  0        0.998      2015
2  California    1        0.125      2015
3  Texas         2        0.124      2019

I've tried using a for loop to iterate through each row, but am unable to get it to work.我尝试使用for循环遍历每一行,但无法使其工作。 How would you achieve this?你将如何实现这一目标?

You can first use wide_to_long on your state_lookup df so you can perform a merge :您可以先在state_lookup df 上使用wide_to_long以便执行merge

s = pd.wide_to_long(state_lookup,stubnames="code",sep="_",i="state",j="year",suffix="\d").reset_index()
s.columns = ["state","code","year"] #rename the columns properly

print (df_states.merge(s, on=["state","code"],how="left"))

          state  code     score  year
0         Texas     0  0.753549  2014
1  Pennsylvania     0  0.998119  2015
2    California     1  0.125751  2015
3         Texas     2  0.125751  2019

Load dataframes加载数据帧

df_states = pd.DataFrame({'state':['Texas','Pennsylvania','California','Texas'],'code':[0,0,1,2], 'score':[0.753549,0.998119,0.125751,0.12575]})
state_lookup = pd.DataFrame({'state':['Texas','Pennsylvania','California'],'code_0': [2014,2015,2014],'code_1': [2015,2016,2017] , 'code_2': [2019,2017,2019]})

First use melt to convert your code_ columns into rows首先使用melt您转换code_列成行

melted_lookup = pd.melt(state_lookup,
                        id_vars=['state'],
                        value_vars=[col for col in state_lookup.columns if col.startswith('code_')], 
                        var_name='new_code',
                        value_name='year')

Then merge the two dataframes:然后合并两个数据帧:

df_states['new_code'] = "code_"+ df_states.code.astype('str') 

df_states = pd.merge(df_states, melted_lookup, how = 'left', on =['new_code','state'])

#   state        code   score      new_code year
#0  Texas           0   0.753549    code_0  2014
#1  Pennsylvania    0   0.998119    code_0  2015
#2  California      1   0.125751    code_1  2017
#3  Texas           2   0.125750    code_2  2019

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas - 根据另一个填充一个数据框列 - Pandas - populate one dataframe column based on another Pandas 根据另一个数据框中的匹配列填充新的数据框列 - Pandas populate new dataframe column based on matching columns in another dataframe 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe 如何在现有 pandas dataframe 中填充新列 - How to populate a new column in an existing pandas dataframe 搜索和查找 从一个 dataframe 到另一个 dataframe 搜索值并根据 pandas 中的查找值填充新列 - Search and lookup Search values from one dataframe in another dataframe and populate new column based on look up values in pandas 遍历一个数据框中的单个列与另一个数据框中的列进行比较使用熊猫在第一个数据框中创建新列 - loop through a single column in one dataframe compare to a column in another dataframe create new column in first dataframe using pandas 使用 Pandas 使用来自一个数据帧的值来压缩数据帧以填充新的数据帧 - Use pandas to condense a dataframe using values from one dataframe to populate a new dataframe 使用另一个数据框创建熊猫数据框列 - Create pandas dataframe column using another dataframe 使用 split 填充 Pandas 数据框中的列 - Using split to populate a column in Pandas dataframe 如果两个单元格值与 pandas 中的另一个较小子集 dataframe 匹配,则使用 True 填充新的 dataframe 列 - Populate a new dataframe column with True if two cell values match another smaller subset dataframe in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM