python - 根据无序日期在 pandas 中创建日期时间对象

标签 python python-3.x pandas datetime

我在 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/

相关文章:

python - 从数据库中检索数据并设置为 Kivy 中的文本输入字段和图像小部件,用于多屏应用程序!属性错误

python - QButtonGroup 不使复选框独占

python - 如何移动数据框的日期?

python - Pandas DataFrame 中的 latex 公式

python - 如何在 Django 管理仪表板中构建链式过滤器

python - 异常处理 - 如何处理用户输入中的无效数据类型?

python - 根据条件连接列表中的元素

python-3.x - Wordpress REST API、Oauth1 和 python 请求不起作用

python - PyInstaller 无法包含 C :\Python27\Lib 中的某些模块

python - 将 pandas 数据框转换为 json,以列为键