我试图导入两个.csv文件,其中包含挪威平均收入的统计数据,与消费价格指数相比,以查看挪威人的购买力是否逐年下降/增加。
import pandas as pd
# Lese datafilen med riktig separator
data = pd.read_csv('Inntekt, 1995-2023.csv', delimiter="\t", quotechar='"')
# Fjerne ekstra anførselstegn i kolonnenavnene
data.columns = [col.replace('"', '') for col in data.columns]
# Skriv ut alle kolonnene for å sjekke om de ble lest inn riktig
print(data.columns)
# Hvis 'year' er i kolonnene, sett det som indeks
if 'year' in data.columns:
data['year'] = data['year'].astype(int)
data.set_index('year', inplace=True)
else:
print('Ingen kolonner har ' + 'year ', 'i seg.')
更新
import pandas as pd
import matplotlib.pyplot as plt
# Import data
kpi_data = pd.read_csv('KPI-JEL, 1995-2023.csv')
income_data = pd.read_csv('Inntekt, 1995-2023.csv')
# Set year as the index for both datasets
kpi_data.set_index('year', inplace=True)
income_data.set_index('year', inplace=True)
# Convert columns to numeric
kpi_data['KPI-JEL (2015=100) årlig'] = pd.to_numeric(kpi_data['KPI-JEL (2015=100) årlig'], errors='coerce')
income_data['Indeks, faste prisar'] = pd.to_numeric(income_data['Indeks, faste prisar'], errors='coerce')
# Normalize the data by dividing with the value in the base year (2015)
kpi_data = kpi_data / kpi_data.loc[2015]
income_data = income_data / income_data.loc[2015]
# Calculate purchasing power
purchasing_power = income_data / kpi_data
# Plot KPI, income, and purchasing power over time
plt.figure(figsize=(10,6))
plt.plot(kpi_data['KPI-JEL (2015=100) årlig'], label='KPI')
plt.plot(income_data['Indeks, faste prisar'], label='Income')
plt.plot(purchasing_power['Indeks, faste prisar'], label='Purchasing Power')
plt.legend()
plt.xlabel('Year')
plt.ylabel('Index (2015=100)')
plt.title('Change in KPI, Income, and Purchasing Power over Time')
plt.show()
数据
The first csv (Average income, hushaldningstype = household type, indeks, faster prisar = index, set prices)
"hushaldningstype","year","Indeks, faste prisar"
"Alle hushald","1995",99
"Alle hushald","1996",99
"Alle hushald","1997",103
"Alle hushald","1998",110
The second csv (CPI, konsumgruppe = consumer group, KPI-JEL is CPI excluding electricity)
"konsumgruppe","year","KPI-JEL (2015=100) årlig"
"KPI-JEL Totalindeks","1995",67.8
"KPI-JEL Totalindeks","1996",68.7
"KPI-JEL Totalindeks","1997",70.2
"KPI-JEL Totalindeks","1998",72.0
"KPI-JEL Totalindeks","1999",73.8
1条答案
按热度按时间jtjikinw1#
IIUC,您可以用途:
输出: