[英]How to append python pandas series columns to dataframe?
I am a newbie on python pandas.我是 python 熊猫的新手。 I have a question on handling pandas dataframe.
我有一个关于处理熊猫数据框的问题。 I use FRED (Federal Reserve Economic Data - St. Louis Fed) python api to get the big data.
我使用 FRED (Federal Reserve Economic Data - St. Louis Fed) python api 来获取大数据。 Below are the sources.
以下是来源。
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())
The above codes print the following results.上面的代码打印以下结果。
******************
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
My target format of results are feature matrix like below with time-series indexing,我的目标结果格式是具有时间序列索引的特征矩阵,如下所示,
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
I make python codes but the results are not satisfactory,我做了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
I want to know how to insert the pandas series value into the middle of pandas dataframe columns.我想知道如何将 Pandas 系列值插入到 Pandas 数据框列的中间。 Any reply will be thankful.
任何答复将不胜感激。
== Update part == 更新部分
I add a few lines for your understanding.我添加几行以供您理解。
df_poverty = df_poverty.loc[mask_poverty,['id', 'title', 'frequency_short', 'seasonal_adjustment_short']]
print(df_poverty)
The print line shows the following results打印行显示以下结果
id title frequency_short seasonal_adjustment_short
series id
PPAAAK02000A156NCEN PPAAAK02000A156NCEN test1 M NSA
Then the my expected feature is to be like below,然后我预期的功能如下所示,
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
Since you have duplicate columns(two columns with the same content), I recommend you remove one.由于您有重复的列(两列内容相同),我建议您删除一列。
df_poverty = df_poverty.drop("series id", 1)
df_poverty["time"] = DatetimeIndex.strftime(%Y-%m-%d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.