Python 使用pands进行同环比的计算
343
4: months = pd.date_range(start='2021-01-01', end='2022-12-31', freq='M')
该句话为生成按月输出的数据
1231
import pandas as pd
import numpy as np
import openpyxl
months = pd.date_range(start='2021-01-01', end='2022-12-31', freq='M')
test_df = pd.DataFrame({'month': months,
'v': 100*np.random.rand(months.shape[0], 1).reshape(months.shape[0])})
test_df["last_month_v"]=test_df['v'].shift(1)
test_df["last_year_v"]=test_df['v'].shift(12)
test_df.to_excel('path_to_file.xlsx', sheet_name='Sheet1')
#test_df['year_erlier_1']=test_df['v']/test_df['last_year_v']-1
print(test_df)
#data = pd.read_excel("test.xlsx")
#print(data.head())