How to add values to a new column based on a list with values from another column

BryceSoker

So let's say I have something like this:~

A    
v1   
v2
v3
v3
v4

And i have list that's like this:

python_list_1 = ["v1","v2"]
python_list_2 = ["v3","v4"]

And now I want to make something along these likes:

df['A'] = np.where(df['B'] in python_list_1 , "XT")
df['A'] = np.where(df['B'] in python_list_2 , "AB")

A    B
v1   XT
v2   XT
v3   AB
v3   AB
v4   AB 

Any tips for a pandas newbie?

jezrael

Use numpy.select with isin for conditions, also is possible set default value if some value not matched both conditions:

print (df)
    A
0  v1
1  v2
2  v3
3  v3
4  v4
5  v5

python_list_1 = ["v1","v2"]
python_list_2 = ["v3","v4"]

m1 = df['A'].isin(python_list_1)
m2 = df['A'].isin(python_list_2)
df['B'] = np.select([m1, m2], ["XT", "AB"], default=df['A'])
print (df)
    A   B
0  v1  XT
1  v2  XT
2  v3  AB
3  v3  AB
4  v4  AB
5  v5  v5

df['B'] = np.select([m1, m2], ["XT", "AB"], default='no match')
print (df)
    A         B
0  v1        XT
1  v2        XT
2  v3        AB
3  v3        AB
4  v4        AB
5  v5  no match

numpy alternative with numpy.in1d:

m1 = np.in1d(df['A'], python_list_1)
m2 = np.in1d(df['A'], python_list_2)
df['B'] = np.select([m1,m2],[ "XT", "AB"], default='no match')
print (df)
    A         B
0  v1        XT
1  v2        XT
2  v3        AB
3  v3        AB
4  v4        AB
5  v5  no match

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add new column based on values of another column from a dictionary in pandas

How to add new column with values based on another column in pandas

How to Add a New Column With Selected Values from Another Column In Python

How to add new column from another dataframe based on values in column of first dataframe?

add a new column in pyspark dataframe based on matching values from a list

How to add a new column in one table based on values from another table in Mysql?

How to add a new column with custom values, based on a WHERE clause from another table in PowerBi?

How do I add a column based on a condition and assign values to it based on the syntax of values from another column?

How to create new values in a pandas dataframe column based on values from another column

how to divide pos and negative values from one column to value from another column and add the result in new column

How to add a column which does a string addition of values from other column based on condition from another column

How to restrict values in a column based on values from another column in PostgreSQL?

How to get values from a dict into a new column, based on values in column

Creating new column based on string values from another column

create new column with values from another column based on condition

Add new column to temp table based on values from a column

Create new column based on values of another column

How can I add a new column based on NA values of another column?

How can I add a label in a new column in pandas based on two consecutive values of another column?

How to generate new column with values based on condition in another column in pandas

How to move values into new columns based on values in another column

Add values in column based on another column

How to add different values in a column based on another column

How to add seperate column values to another column based on group by in R?

How to add a new column in the middle of the dataframe with values based on the previous column?

Add new columns and insert values in columns based on value in another column

How to create new columns with count values based on the values from another column SQL

How to resample df based on one column and add the values from another column?

Add a new column to a numpy array with values based on list of indices

TOP Ranking

HotTag

Archive