Skip to content Skip to sidebar Skip to footer

How To Extract Certain Under Specific Condition In Pandas? (Sentimental Analysis)

The picture is what my dataframe looks like. I have user_name, movie_name and time column. I want to extract only rows that are first day of certain movie. For example, if movie a

Solution 1:

I assume that time column is of datetime type. If not, convert this column calling pd.to_datetime.

Then run:

df.groupby('movie_name').apply(lambda grp:
    grp[grp.time.dt.date == grp.time.min().date()])

Groupby groups the source DataFrame into grops concerning particular films.

Then grp.time.min().date() computes the minimal (first) date from the current group.

And finally the whole lamda function returns only rows from this date (also from the current group).

The same for other groups of rows (films).


Post a Comment for "How To Extract Certain Under Specific Condition In Pandas? (Sentimental Analysis)"