根据DateTime列增加年度通货膨胀列



这是我在数据框架上的DateTime列,我想根据DateTime列上的'year'添加两个不同的列,如' annual Inflation Rate of UK'和' annual Inflation Rate of Turkey'。我需要你的帮助如何在python上做到这一点

输入:

| DateTime |
|14.08.2014|  
|15.07.2015| 
|16.06.2016|

所需输出:

tbody> <<tr>
DateTime英国通货膨胀土耳其通货膨胀
14.08.20142.368.85
15.07.20150.377.67
16.06.20161.017.78

示例

data = {' DateTime ': {0: '14.08.2014', 1: '15.07.2015', 2: '16.06.2016'}}
df = pd.DataFrame(data)

df

DateTime
0   14.08.2014
1   15.07.2015
2   16.06.2016

out = df.assign(Inflation_of_UK=[2.36, 0.37, 1.01], Inflation_of_Turkey=[8.85, 7.67, 7.78])

out

DateTime    Inflation_of_UK Inflation_of_Turkey
0   14.08.2014  2.36            8.85
1   15.07.2015  0.37            7.67
2   16.06.2016  1.01            7.78

试试这个

df['Year'] = pd.DatetimeIndex(df['DateTime'). 
df = df.merge(inflation_rates, left_on='Year', right_on='Year', how='left')
df.rename(columns={'UK_Inflation_Rate': 'Inflation of UK', 'Turkey_Inflation_Rate': 'Inflation of Turkey'}, inplace=True)
df.drop('Year', axis=1, inplace=True)
print(df)

最新更新