简体   繁体   English

如何使用DatetimeIndex切片pandas Series并将其放入DataFrame中?

[英]How do i slice pandas Series with DatetimeIndex and put it in a DataFrame by rows?

I have pandas Series object with DatetimeIndex: 我有使用DatetimeIndex的pandas Series对象:

2016-01-04 00:00:00    11.6
2016-01-04 01:00:00    11.7
2016-01-04 02:00:00    17.1
2016-01-04 03:00:00    36.5
2016-01-04 04:00:00    19.0
2016-01-04 05:00:00     7.5
2016-01-04 06:00:00    18.7
2016-01-04 07:00:00    14.5
2016-01-04 08:00:00     6.5
2016-01-04 09:00:00    41.9
2016-01-04 10:00:00    37.3
2016-01-04 11:00:00    30.3
2016-01-04 12:00:00    38.4
2016-01-04 13:00:00    26.0
2016-01-04 14:00:00    31.0
2016-01-04 15:00:00    32.3
2016-01-04 16:00:00    29.4
2016-01-04 17:00:00    43.5
2016-01-04 18:00:00    26.9
2016-01-04 19:00:00    20.4
2016-01-04 20:00:00    16.5
2016-01-04 21:00:00    10.9
2016-01-04 22:00:00    12.8
2016-01-04 23:00:00    16.4
2016-01-05 00:00:00    10.1
2016-01-05 01:00:00     9.4
2016-01-05 02:00:00    12.0
2016-01-05 03:00:00     8.6
2016-01-05 04:00:00    16.9
2016-01-05 05:00:00     8.4
...

How i can slice this series by dates and put it in a dates/hours pandas Dataframes like this: 我如何按日期切片这个系列,并把它放在一个日期/小时pandas Dataframes像这样:

              00   01   02   03   04 ...   22   23    
2016-01-04  11.6  1.7 17.1 36.5 19.0 ... 12.8 16.4
2016-01-05  10.1  9.4 12.0  8.6 16.9 ... 12.8 16.4
...

PS: Sorry my English PS:对不起我的英文

First convert your series to a dataframe with to_frame , then extract the hours and date with s.index.strftime('%H') and index.date . 首先使用to_frame将系列转换为数据to_frame ,然后使用s.index.strftime('%H')index.date提取小时和日期。 Finally pivot on these column to convert to "wide" format: 最后在这些列上转动以转换为“宽”格式:

df = s.to_frame()
df['hour'] = df.index.strftime('%H')
df['date'] = df.index.date
df2 = df.pivot(columns='hour', index='date')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pandas DataFrame DatetimeIndex 切片错误 - pandas DataFrame DatetimeIndex slice error 如何在数据框中正确设置 Pandas 日期时间对象的日期时间索引? - How do I properly set the Datetimeindex for a Pandas datetime object in a dataframe? 使用Pandas仅移动DatetimeIndex系列的一部分并将其放回到向前移动的文件中 - Using Pandas to shift only a slice of a DatetimeIndex series and put it back in to the file shifted ahead 在DatetimeIndex上使用pandas时间戳切片DataFrame - Slice DataFrame using pandas Timestamp on DatetimeIndex 我如何从Pandas和Python中的特定标签索引开始的Dataframe中切片X行 - How do I slice X rows from a Dataframe begining at a specific label index in Pandas and Python 如何在索引中没有的日期对 pandas 时间序列进行切片? - How do I slice a pandas time series on dates not in the index? 从熊猫DataFrame或系列插值到新的DatetimeIndex - Interpolating from a pandas DataFrame or Series to a new DatetimeIndex 如何将 Pandas DataFrame 中的退货系列转换为价格系列? - How do I convert return series in Pandas DataFrame to price series? 如何根据条件切割pandas数据帧? - How do I slice a pandas dataframe based on a condition? 如何在熊猫上切片Multindex时间序列数据帧? - How do I slice a Multindex timeseries dataframe on Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM