Skip to main content
 首页 » 编程设计

python之Pandas 时序数据索引从字符串到 float

2024年09月07日25jillzhang

<分区>

谁知道如何将字符串输出转换为 float ?我正在尝试根据时间戳索引为 "Month" 和 "day of the week" 创建单独的数据帧。 df.index.strftime 输出一个字符串,但我需要一个基于月份数值(从 1 月到 12 月为 0-11 或 1-12)的 float 和星期几数值(周日至周六为 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