熊猫数据框转换为月度数据,但在日期和日期基础上增加另一列

时间:2019-03-07 10:53:43

标签: pandas python-2.7

我有以下数据:

Data snapshot

我想将此数据转换为每月数据,以便其显示如下:

`       In_Account      Amount`

'Date                     '
Jan       True          xxx.x
Jan       False         yyy.y
Feb       True          zzz.z

以此类推

1 个答案:

答案 0 :(得分:0)

尝试一下:

import pandas as pd, datetime, calendar

# begin example data setup; you'll probably just read this in from CSV or XLS
data = [['2018-11-20 00:00:00', 12141521 , True, 1922],
        ['2018-10-03 00:00:00', 1083287, True, 98],
        ['2018-11-30 00:00:00', -6400, True, 2327]]
df = pd.DataFrame(data,columns=['Date', 'Amount', 'In_Account', 'JV Number'])
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d %H:%M:%S')
# end examaple data setup

for index, row in df.iterrows():
    d = row['Date']
    m = datetime.datetime.strptime(str(d), '%Y-%m-%d %H:%M:%S').month
    df.at[index, 'Month'] = calendar.month_abbr[int(m)]

print df