[英]In a pandas Dataframe how to change the acronym for month to number month?
[英]how to change the month name column to month number column in dataframe in pandas
我有一列名為 Jan, Feb , Mar.... 的列名為 Month number ...,我想將整個列更改為數字,例如:01,02,03...如何在 python 中實現,當我使用 strptime 時,得到像“strptime() 參數 1 必須是 str,而不是系列”這樣的錯誤
假設以下 df:
from time import strptime
df = pd.DataFrame({'month':['Jan','Feb','Mar']})
month
0 Jan
1 Feb
2 Mar
您可以使用strptime(x, '%b')
將縮寫的月份名稱轉換為數字。
df['month_number'] = [strptime(str(x), '%b').tm_mon for x in df['month']]
結果:
month month_number
0 Jan 1
1 Feb 2
2 Mar 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.