I am new to python. I am trying to subset data from a pandas dataframe using values present in a list. Below is a simple example of what I am trying to do.
import pandas as pd
# Create dataframe df which contains only one column having weekdays as values
df = pd.DataFrame({'days':['monday','tuesday','wednesday','thursday','friday']})
# A list containing all seven days of a week
day_list = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']
# Create a new dataframe which should contain values present in list but missing in dataframe
df1 = df[~df.days.isin(day_list)]
# Output shows empty dataframe
Empty DataFrame
Columns: [days]
Index: []
# This gives error
df2 = df[~day_list.isin(df.days)]
# output from df2 code execution
df2 = df[~day_list.isin(df.days)]
AttributeError: 'list' object has no attribute 'isin'
In R, I can easily get this result using the below condition.
# Code from R
df1 <- day_list[! (day_list %in% df$days), ]
I want to create a new dataframe which contains only those values present in the list day_list
but not present in df.days
. In this case, it should return 'saturday' and 'sunday' as output. How can I get this result? I have looked at the solution provided in this thread - How to implement 'in' and 'not in' for Pandas dataframe. But it is not solving my problem. Any guidance on this to a Python 3.x newbie would really be appreciated.