gpt4 book ai didi

python - 从数据框中的列中提取星期几并放入另一列中

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:06 25 4
gpt4 key购买 nike

# Give day of the week
def DOW(df):
DOW = pd.Series(datetime.datetime.strptime(df['indx'],'%Y%m%d').strftime('%A'))
df = df.join(DOW)
return df

我从另一个脚本调用此函数,其中 d 是我传递给函数 DOW 的数据帧

 d = TA.DOW(d)

它抛出错误。同样的问题有什么解决办法

DOW=pd.Series(datetime.datetime.strptime(df['indx'],'%Y%m%d').strftime('%A'))
TypeError: must be string, not Series

最佳答案

我认为你可以先转换列indx to_datetime然后使用 dt.strftime如前所述EdChum :

print df
indx Value
0 20020101 3.00
1 20020102 3.50
2 20020103 3.30
3 20100101 4.96
4 20100102 4.98

df['new'] = pd.to_datetime(df['indx'], format='%Y%m%d').dt.strftime('%A')
print df
indx Value new
0 20020101 3.00 Tuesday
1 20020102 3.50 Wednesday
2 20020103 3.30 Thursday
3 20100101 4.96 Friday
4 20100102 4.98 Saturday

关于python - 从数据框中的列中提取星期几并放入另一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36497782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com