我是python熊猫的新手。我有一个关于处理熊猫数据帧的问题。我使用FRED(美联储经济数据-圣路易斯联储)python api获取大数据。以下是资料来源
df_poverty = fred.search(search_str, order_by='title') # returns pandas dataframe
mask_poverty = df_poverty.title == search_str
df_poverty = df_poverty.loc[mask_poverty,['id']]
if not df_poverty.empty:
df_poverty_tmp = fred.get_series(df_poverty.iloc[0].id) # returns another pandas dataframe
print('******************')
print(df_poverty.index)
print('==================')
print(df_poverty.head())
print('==================')
print(df_poverty_tmp.index)
print('==================')
print(df_poverty_tmp.head())
上述代码打印以下结果
******************
Index(['PPAAAR05000A156NCEN'], dtype='object', name='series id')
==================
id
series id
PPAAAR05000A156NCEN PPAAAR05000A156NCEN
==================
DatetimeIndex(['1989-01-01', '1990-01-01', '1991-01-01', '1992-01-01',
'1993-01-01', '1994-01-01', '1995-01-01', '1996-01-01',
'1997-01-01', '1998-01-01', '1999-01-01', '2000-01-01',
'2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01',
'2005-01-01', '2006-01-01', '2007-01-01', '2008-01-01',
'2009-01-01', '2010-01-01', '2011-01-01', '2012-01-01',
'2013-01-01', '2014-01-01', '2015-01-01', '2016-01-01',
'2017-01-01', '2018-01-01'],
dtype='datetime64[ns]', freq=None)
==================
1989-01-01 17.9
1990-01-01 NaN
1991-01-01 NaN
1992-01-01 NaN
1993-01-01 18.9
dtype: float64
我的目标结果格式是特征矩阵,如下所示,带有时间序列索引
1989-01-01 PPAAAR05000A156NCEN 17.9
1990-01-01 PPAAAR05000A156NCEN NaN
1991-01-01 PPAAAR05000A156NCEN NaN
1992-01-01 PPAAAR05000A156NCEN NaN
1993-01-01 PPAAAR05000A156NCEN 18.9
我编写了python代码,但结果并不令人满意
> df_poverty_tmp.append(df_poverty)
0 id
1989-01-01 00:00:00 17.7 NaN
1990-01-01 00:00:00 NaN NaN
1991-01-01 00:00:00 NaN NaN
1992-01-01 00:00:00 NaN NaN
1993-01-01 00:00:00 18.8 NaN
1994-01-01 00:00:00 NaN NaN
1995-01-01 00:00:00 17.6 NaN
1996-01-01 00:00:00 16.7 NaN
1997-01-01 00:00:00 16.2 NaN
1998-01-01 00:00:00 15.7 NaN
PPAAAL01000A156NCEN NaN PPAAAL01000A156NCEN
我想知道如何将pandas系列值插入pandas数据框列的中间。任何答复都将不胜感激
==更新部分
我补充几行让你理解
df_poverty = df_poverty.loc[mask_poverty,['id', 'title', 'frequency_short', 'seasonal_adjustment_short']]
print(df_poverty)
打印行显示以下结果
id title frequency_short seasonal_adjustment_short
series id
PPAAAK02000A156NCEN PPAAAK02000A156NCEN test1 M NSA
那么我期望的特性如下所示
time id title frequency_short value
1989-01-01 PPAAAR05000A156NCEN test1 M 17.9
1990-01-01 PPAAAR05000A156NCEN test1 M NaN
1991-01-01 PPAAAR05000A156NCEN test1 M NaN
1992-01-01 PPAAAR05000A156NCEN test1 M NaN
1993-01-01 PPAAAR05000A156NCEN test1 M 18.9
由于您有重复的列(两列内容相同),我建议您删除一列
相关问题 更多 >
编程相关推荐