我在 pandas 中有一个数据框,看起来像这样
Year Month Month_index Rainfall
1900 Jan 1 4.8
1901 Jan 1 5
.
.
.
1900 Feb 2 3.2
1901 Feb 2 4.3
.
.
要使用日期时间索引,我需要重新排列它,使其看起来像 -
Year Month Month_index Rainfall
1900 Jan 1 4.8
1900 Feb 2 3.2
.
.
1901 Jan 1 5
1901 Feb 2 4.3
.
.
当然,还有完整的 12 个月,为了简洁起见,我只展示了一点点。我对 python 相当陌生,所以我不知道是否有一个命令可以做到这一点。 先感谢您!
编辑:这是我迄今为止使用的代码 -
import csv
#import pyexcel-io as pi
import numpy as np
import pandas as pd
import dateutil
#Read data from csv file into dataframe
df =pd.read_csv('/Users/Gingeraffe/Documents/University/3rd_year/Bureau_Research/Notebooks/Data/rainfall_SW_WA.csv')
months = df.columns[1:]
#Melt is putting months down a column and the data down another column.
Problem is ' jan jan jan... feb feb feb..' etc. instead of 'jan feb mar.. etc'
df = pd.melt(df, id_vars='Year', value_vars=months, var_name='Month')
df.insert(2,'Month_index',0)
M = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'June':6, 'July':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12}
df.Month_index = df.Month.map(M)
最佳答案
使用您的三列创建一个datetime
系列,然后我们可以用它来排序:
s = pd.to_datetime(
df[['Year', 'Month']].astype(str).sum(1), format='%Y%b'
)
最后,排序:
df.iloc[s.sort_values().index]
Year Month Month_index Rainfall
0 1900 Jan 1 4.8
2 1900 Feb 2 3.2
1 1901 Jan 1 5.0
3 1901 Feb 2 4.3
关于python - 根据无序日期在 pandas 中创建日期时间对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52251023/