【发布时间】:2018-07-15 13:51:04
【问题描述】:
有人知道如何将字符串输出转换为浮点数吗?我正在尝试根据时间戳索引为"Month" 和"day of the week" 创建单独的数据帧。 df.index.strftime 输出一个字符串,但我需要一个 float 基于月份数值(1 月至 12 月为 0-11 或 1-12)和一周中的某一天数值(0-6 或 1-7周日到周六)
from numpy.random import randint
import pandas as pd
# Create a df with a date-time index with data every 6 hours
rng = pd.date_range('1/5/2018 00:00', periods=5, freq='6H')
df = pd.DataFrame({'Random_Number':randint(1, 10, 5)}, index=rng)
# Getting different time information in the columns
df['month'] = df.index.strftime('%b')
df['Day_of_week'] = df.index.strftime('%a')
df
【问题讨论】:
标签: python pandas time-series