Raha_Tiny
Raha_Tiny

Reputation: 11

Extract a row while looking for a string in a csv file

I have a CSV file including 2 million rows and 2 million columns. One of the columns is the date and I want to extract all the rows which the date columns contain a "Monday" as a string, how can I do it? I can also change the date into the number format (mm/dd/yyyy) and then find all the rows (including their columns) which indicate a Monday.

Upvotes: 1

Views: 87

Answers (1)

darunia
darunia

Reputation: 11

For future reference, it is best practice to pose your question with the minimum reproducible code. I'm new here as well, so, don't sweat it!

To answer your question, you can pass a condition into a DataFrame on reassignment. I believe this is the most direct way of accomplishing your goal.

df = df['monday' in df['Date'].str.lower]

This answer filters the dataframe so that only rows containing the word "Monday" remain. Notice, calling .lower() directly on the df['Date'] will yield a type error, which is why it is imperative you first cast to a string. Of course, if you know the case of every occurrence of the word "Monday", calling .lower() isn't necessary in the first place.

Note, if you want to use this approach the check multiple conditions, you must used the bitwise operators (&, |, ^). As the boolean operators (and, or) will yield a type error.

Upvotes: 1

Related Questions