我有一个pandas数据framedf
,看起来像这样。
的数据
import pandas as pd
url = "https://www-genesis.destatis.de/genesisWS/rest/2020/data/tablefile?username=DEB924AL95&password=P@ssword1234&name=42153-0002&area=all&compress=false&transpose=false&startyear=1900&endyear=×lices=®ionalvariable=®ionalkey=&classifyingvariable1=WERT03&classifyingkey1=BV4TB&classifyingvariable2=WZ08V2&classifyingkey2=&classifyingvariable3=&classifyingkey3=&format=xlsx&job=false&stand=01.01.1970&language=en"
df = pd.read_excel(url, engine='openpyxl')
df = df.iloc[5:-3]
df.columns = ['Variable', 'Date', 'Value']
m = df['Date'].isna()
df['Date'] += '-' + df['Variable'].ffill()
df['Variable'] = df['Variable'].where(m).ffill()
df
import numpy as np
# Reshape your dataframe
out = (df[~m].replace('...', np.nan)
.pivot_table(index='Date', columns='Variable',
values='Value', sort=False)
.reset_index().rename_axis(columns=None))
out
字符串
这只给了我一个Date
列,没有值。
Date
0 January-1991
1 February-1991
2 March-1991
3 April-1991
型
我需要做什么改变才能使它工作?
1条答案
按热度按时间l7wslrjt1#
问题是你使用数值聚合
pivot_table
(默认为mean
),但你的数据不是数值。首先用to_numeric
转换:字符串
输出摘录:
型