Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: assign value depending on another dataframe

Tags:

python

pandas

I have to dataframes that look like this:

 df1:          condition        
               A               
               A              
               A               
               B              
               B             
               B           
               B   

 df2:          condition      value   
               A               1
               B               2

I would like to assign to each condition its value, adding a column to df1 in order to obtain:

 df1:          condition     value      
               A               1
               A               1
               A               1
               B               2
               B               2
               B               2
               B               2

how can I do this? thank you in advance!

like image 260
Luca91 Avatar asked Oct 08 '18 13:10

Luca91


People also ask

How do you assign value from one data frame to another?

assign() method assign new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original ones. Existing columns that are re-assigned will be overwritten. Length of newly assigned column must match the number of rows in the dataframe.

How do I get the value of a column in a DataFrame based on another column?

You can extract a column of pandas DataFrame based on another value by using the DataFrame. query() method. The query() is used to query the columns of a DataFrame with a boolean expression.

How to get values from another Dataframe in pandas?

You can use Pandas merge function in order to get values and columns from another DataFrame. For this purpose you will need to have reference column between both DataFrames or use the index.

How to assign to multiple columns at once in pandas?

As mentioned in the comments, you can also do the assignment to both columns in one shot: Note that you'll need pandas version 0.11 or newer to make use of loc for overwrite assignment operations. Another way to do it is to use what is called chained assignment.

How do you filter DataFrames in pandas?

Pandas’ loc creates a boolean mask, based on a condition. Sometimes, that condition can just be selecting rows and columns, but it can also be used to filter dataframes. These filtered dataframes can then have values applied to them.

How do I apply a label to another column in pandas?

Using Pandas Map to Set Values in Another Column The Pandas.map () method is very helpful when you’re applying labels to another column. In order to use this method, you define a dictionary to apply to the column. For our sample dataframe, let’s imagine that we have offices in America, Canada, and France.


Video Answer


1 Answers

Use map by Series created by set_index if need append one column only:

df1['value'] = df1['condition'].map(df2.set_index('condition')['value'])

print (df1)
  condition  value
0         A      1
1         A      1
2         A      1
3         B      2
4         B      2
5         B      2
6         B      2

Or use merge with left join if df2 have more columns:

df = df1.merge(df2, on='condition', how='left')
print (df)
  condition  value
0         A      1
1         A      1
2         A      1
3         B      2
4         B      2
5         B      2
6         B      2
like image 196
jezrael Avatar answered Nov 15 '22 02:11

jezrael