用Python将数据转换为增长率

2024-10-02 22:24:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个变量,我想用月增长率来表示其中一个变量(货币基础)。 我该怎么做?。在R语言中,您应该首先将数据转换为时间序列,在Python中也是这样吗

#LLamando a las series que buscamos    
inflacion = llamada_api('https://api.estadisticasbcra.com/inflacion_mensual_oficial')
base_monetaria = llamada_api('https://api.estadisticasbcra.com/base')

#Armando DataFrames
df = pd.DataFrame(inflacion)
df_bm = pd.DataFrame(base_monetaria)

#Renombrando columnas
df = df.rename(columns={'d':'Fecha',
                        'v':'IPC'})

df_bm = df_bm.rename(columns={'d':'Fecha',
                              'v':'base_monetaria'})

#Arreglando tipo de datos
df['Fecha']=pd.to_datetime(df['Fecha'])
df_bm['Fecha']=pd.to_datetime(df_bm['Fecha'])

#Verificando que las fechas esten en formato date
df['Fecha'].dtype
df_bm['Fecha'].dtype

#Filtrando 
df_ipc = df[(df['Fecha'] > '2002-12-31')]
df_bm_filter = df_bm[(df_bm['Fecha'] > '2002-12-31')]

#Graficando
plt.figure(figsize=(14,12))
df_ipc.plot(x = 'Fecha', y = 'IPC')
plt.title('IPC-Mensual', fontdict={'fontsize':20})
plt.ylabel('IPC')
plt.xticks(rotation=45)
plt.show()

数据如下所示

         Fecha  base_monetaria
1748 2003-01-02           29302
1749 2003-01-03           29360
1750 2003-01-06           29524
1751 2003-01-07           29867
1752 2003-01-08           29957
        ...             ...
5966 2020-02-18         1941302
5967 2020-02-19         1941904
5968 2020-02-20         1887975
5969 2020-02-21         1855477
5970 2020-02-26         1807042

这样做的目的是获取当月最后一天的数据,并使用上月最后一天的数据计算增长率


Tags: 数据apidfbasepltlasbmque
3条回答

你可以试试这样的

from pandas.tseries.offsets import MonthEnd
import pandas as pd

df = pd.DataFrame({'Fecha': ['2020-01-31', '2020-02-29', '2020-03-31', '2020-05-31', '2020-04-30', '2020-07-31', '2020-06-30', '2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30', '2020-12-31'],
                   'price': ['32132', '54321', '3213121', '432123', '32132', '54321', '32132', '54321', '3213121', '432123', '32132', '54321']})

df['Fecha'] = df['Fecha'].astype('datetime64[ns]')
df['is_month_end'] = df['Fecha'].dt.is_month_end
df = df[df['is_month_end'] == True]

df.sort_values('Fecha',inplace=True)

df.reset_index(drop=True, inplace = True)

def change(x,y):
    try:
        index = df[df['Fecha']==y].index.item()
        last = df.loc[index-1][1]
        return float(x)/float(last)
    except:
        return 0


df['new_column'] = df.apply(lambda row: change(row['price'],row['Fecha']), axis=1)

df.head(12)

假设base_moetaria是一个月累积值,那么

df = pd.DataFrame({'Fecha': ['2020-01-31', '2020-02-29', '2020-03-31', '2020-05-31', '2020-04-30', '2020-07-31', '2020-06-30', '2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30', '2020-12-31'],
                   'price': [32132, 54321, 3213121, 432123, 32132, 54321, 32132, 54321, 3213121, 432123, 32132, 54321]})

df['Fecha'] = pd.to_datetime(df['Fecha'])
df.set_index('Fecha', inplace=True)
new_df = df.groupby(pd.Grouper(freq="M")).tail(1).reset_index()
new_df['rate'] = (new_df['price'] -new_df['price'].shift(1))/new_df['price'].shift(1)

新的_df['rate']将按照您在下面评论中解释的方式为您提供增长率

这个问题可以通过使用base_monetaria的滞后值创建列来解决

df_bm_filter['is_month_end'] = df_bm_filter['Fecha'].dt.is_month_end
df_last_date = df_bm_filter[df_bm_filter['is_month_end'] == True]
df_last_date['base_monetaria_lag'] = df_last_date['base_monetaria'].shift(1)
df_last_date['bm_growth'] = (df_last_date['base_monetaria'] - df_last_date['base_monetaria_lag']) / df_last_date['base_monetaria_lag']

相关问题 更多 >