Manipulating Series in Dataframe (Pandas) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Manipulating Series in Dataframe (Pandas) (/thread-17031.html) |
Manipulating Series in Dataframe (Pandas) - wendysling - Mar-25-2019 Hi, I have a date column in a data frame that looks like this: (Year-Month-Day) 2017-09-21 2018-11-25 I am trying to create a function that considers only the year, I have been trying the following. df[df['DateColumn'].str[:4]=='2017'] But I am receiving this error: AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas Any chance to point what I am missing here? Should I change the column to strings, or is there a way to do something similar with the series? Thank you for your help, Wendy RE: Manipulating Series in Dataframe (Pandas) - scidam - Mar-26-2019 Pandas is very flexible, so you access year component of a date-column easily : df.loc[:, 'DateColumn'].dt.year Also, you can select desired rows of the dataframe by year: df.loc[df.loc[:, 'DateColumn'].dt.year == 2017, :] RE: Manipulating Series in Dataframe (Pandas) - wendysling - Mar-26-2019 This worked: df.loc[df.loc[:, 'DateColumn'].dt.year == 2017, :] Thank you! |