PineNuts0
PineNuts0

Reputation: 5234

Python Dataframe: Find if Column Value is in List Variables and Label Accordingly

1) I have a list variable called weekday_list that is comprised of string words separated by commas example: weekday_list = ['Monday', 'Tuesday', 'Wednesday']

2) I also have another similar list variable called weekend_list example: weekend_list = ['Saturday', 'Sunday']

Let's say I have the following dataframe:

Date    Day_of_Week            
1/5     Monday           
1/6     Tuesday          
1/7     Wednesday   
1/8     Thursday    
1/9     Friday   
1/10    Saturday    
1/11    Sunday   

I want to do the following with Python code:

a) Create a new column called "Label" that says 'weekday' if the value in column "Day_of_Week" is in the variable weekday_list

b) says "weekend" if the value in column "Day_of_Week" is in the variable weekend_list

c) finally says NA if the value in column "Day_of_Week" is in neither variable

*Any help is greatly appreciated

Upvotes: 1

Views: 658

Answers (2)

piRSquared
piRSquared

Reputation: 294488

you can create a label_dict instead and use pd.Series.map

label_dict = {}
for wd in weekday_list:
    label_dict[wd] = 'weekday'
for we in weekend_list:
    label_dict[we] = 'weekend'

df.Day_of_Week.map(label_dict)

0    weekday
1    weekday
2    weekday
3        NaN
4        NaN
5    weekend
6    weekend
Name: Day_of_Week, dtype: object

Upvotes: 1

akuiper
akuiper

Reputation: 215057

Assuming you are using pandas, there are a few different ways to do this. Here is an intuitive option. The way it works: use the .isin() method to create a logical index to indicate if the Day_of_Week is in the weekday_list or weekend_list or neither, and then update the Label column accordingly with .loc accessor:

import numpy as np
df["Label"] = np.nan
df.loc[df.Day_of_Week.isin(weekday_list), "Label"] = "weekday"
df.loc[df.Day_of_Week.isin(weekend_list), "Label"] = "weekend"

enter image description here


Another one-liner option would be to use np.where:

df["Label"] = np.where(df.Day_of_Week.isin(weekday_list), "weekday", 
                       np.where(df.Day_of_Week.isin(weekend_list), "weekend", 
                                None))

Upvotes: 1

Related Questions