用Python将数据转换为增长率

用Python将数据转换为增长率,python,pandas,numpy,Python,Pandas,Numpy,我有两个变量,我想用月增长率来表示其中一个变量(货币基础)。 我该怎么做?。在R语言中,您应该首先将数据转换为时间序列,在Python中也是这样吗 #LLamando a las series que buscamos inflacion = llamada_api('https://api.estadisticasbcra.com/inflacion_mensual_oficial') base_monetaria = llamada_api('https://api.estadist

我有两个变量,我想用月增长率来表示其中一个变量(货币基础)。 我该怎么做?。在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
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\u 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']将按照您在下面的评论中解释的方式为您提供增长率

假设
基本_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']将按照您在下面评论中解释的方式为您提供增长率

您可以尝试类似的方法

         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
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)

你可以试试这样的

         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
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_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']

这个问题可以通过使用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']

是每个月的
基本货币
累计值吗?您需要确保
Fecha
列是日期时间。您可以使用
df['Fecha']=pd.to_datetime(df['Fecha'])
来实现这一点。还可以使用
df设置索引的日期时间。set_index('Fecha',inplace=True)
是每个月的
base_monetaria
累积值吗?您需要确保
Fecha
列是日期时间。您可以使用
df['Fecha']=pd.to_datetime(df['Fecha'])
来实现这一点。还可以使用
df设置日期时间索引。set_index('Fecha',inplace=True)
这是可行的,但现在我应该如何表示增长率?增长率的公式是什么?以前/当前时间100?(t期数值)-(t-1期数值)/(t-1期数值)或ln(t期数值)-ln(t-1期数值)这是可行的,但现在我应该如何表示增长率?增长率的公式是什么?上一次/当前时间100?(t期数值)-(t-1期数值)/(t-1期数值)或ln(t期数值)-ln(t-1期数值)您是否更改了“基本货币”的“价格”?,您是否可以将例外部分替换为例外,如e:打印(e)返回0I仅更改基本货币的价格ID您是否更改了“基本货币”的“价格”?,您是否可以将除此之外的部分替换为除此之外的例外为e:打印(e)返回0I仅更改基本货币的价格