Reputation: 4907
I have the panda dataframe in python below.
full_name serial Date_YMD prc1 prc2 volume
bottle_a AX80 20200922 12874.50 12927.75 61023.0
bottle_a AX80 20200923 12878.50 12926.75 61023.0
bottle_a AX80 20200924 12872.50 12928.75 61023.0
bottle_a AX80 20200925 12885.50 12984.25 62295.0
bottle_a AX80 20200926 12880.00 13000.00 14224.0
I want to delete all the rows with Date_YMD
containing Saturdays.
The final dataframe will look like this;
full_name serial Date_YMD prc1 prc2 volume
bottle_a AX80 20200922 12874.50 12927.75 61023.0
bottle_a AX80 20200923 12878.50 12926.75 61023.0
bottle_a AX80 20200924 12872.50 12928.75 61023.0
bottle_a AX80 20200925 12885.50 12984.25 62295.0
To find out whether a row is a Saturday, here is the code;
df['Date_YMD'] = pd.to_datetime(df['Date_YMD'], format='%Y%m%d')
df['Date_YMD'].dt.dayofweek.eq(5)
To drop a row, I believe the drop()
function is the right one to use. If there are better options, please advise.
The problem is how do I apply drop()
after finding out the Saturday rows. I am open to other methods besides using drop()
as long as it solves the problem.
I am using python 3.8
Upvotes: 2
Views: 76
Reputation: 26676
You are on the right track. Just extract day of the week
using strftime
and filter out non Saturdays.
df= df[~pd.to_datetime(df['Date_YMD'], format='%Y%m%d').dt.strftime("%A").isin(['Saturday'])]
or convert days to numbers and drop any that is not equal to 6
df=df[pd.to_datetime(df['Date_YMD'], format='%Y%m%d').dt.strftime("%w")!='6']
full_name serial Date_YMD prc1 prc2 volume
0 bottle_a AX80 20200922 12874.5 12927.75 61023.0
1 bottle_a AX80 20200923 12878.5 12926.75 61023.0
2 bottle_a AX80 20200924 12872.5 12928.75 61023.0
3 bottle_a AX80 20200925 12885.5 12984.25 62295.0
Upvotes: 4