I am fairly new to pandas and getting a problem with manipulating a DataFrame.
What I have is a DataFrame with repeating dates and I would like to only keep the dates which are hourly.
Here is an example of my current DataFrame:
Time ColA ColB
5/12/2011 10:00:00 PM 9 4
5/12/2011 10:15:00 PM 5 3
5/12/2011 10:30:00 PM 1 1
5/12/2011 10:45:00 PM 2 3
5/12/2011 11:00:00 PM 10 4
Thus the result should be a DataFrame that contains the first and last row alone.
Time ColA ColB
5/12/2011 10:00:00 PM 9 4
5/12/2011 11:00:00 PM 10 4
So long as the column is a datetime already you can access the minute atribute and use this to filter:
In [26]:
df[df.Time.dt.minute == 0]
Out[26]:
Time ColA ColB
0 2011-05-12 22:00:00 9 4
4 2011-05-12 23:00:00 10 4
If necessary convert the string to a datetime using: df['Time'] = pd.to_datetime(df['Time')
确保您的列是日期时间(使用pd.to_datetime)而不是字符串。
df = df[df['Time'].apply(lambda x: x.minute) == 0]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.