Pandas连接:自动将时间序列名称用作列名

时间:2017-03-20 10:22:06

标签: python pandas dataframe

我有很多大熊猫时间序列,我想将它们组合成一个数据帧。由它产生的数据帧没有列名。有没有办法在数据帧中重用时间序列的名称(s_a,overflow,s_c,...)而不必明确指定它?

import pandas as pd
import numpy as np

dates = pd.date_range('2017-01-01', '2017-03-01')
s_a = pd.Series(np.random.randn(60), index = dates)
s_b = pd.Series(np.random.randn(60), index = dates)
s_c = pd.Series(np.random.randn(60), index = dates)
s_d = pd.Series(np.random.randn(60), index = dates)
df_a = pd.concat([s_a, s_b, s_c, s_d], join='outer', axis = 1)

我希望有一些事情

s_list = [s_a, s_b, s_c, s_d]

然后在构造数据帧之后应用假设函数s_list.names。

df_a = pd.concat(s_list, join='outer', axis = 1)
df_a.columns = s_list.names()

将生成所需的数据帧。

1 个答案:

答案 0 :(得分:1)

你的系列没有name属性,所以我认为你必须为你的系列指定名称,当你连接时它会很好地作为列名流动。

import pandas as pd
import numpy as np

dates = pd.date_range('2017-01-01', '2017-03-01')
s_a = pd.Series(np.random.randn(60), index = dates,name='s_a')
s_b = pd.Series(np.random.randn(60), index = dates,name='s_b')
s_c = pd.Series(np.random.randn(60), index = dates,name='s_c')
s_d = pd.Series(np.random.randn(60), index = dates,name='s_d')
s_x = pd.Series(np.random.randn(60), index = dates)
df_a = pd.concat([s_a, s_b, s_c, s_d],join='outer', axis = 1)

编辑: 替代解决方案

根据此回答https://stackoverflow.com/a/18425523/5729272,您可以将名称提取到列表中并将其分配给列。

import pandas as pd
import numpy as np
import inspect
def retrieve_name(variables):
    callers_local_vars = inspect.currentframe().f_back.f_locals.items()
    return [var_name for var in variables for var_name, var_val in callers_local_vars if var_val is var]

dates = pd.date_range('2017-01-01', '2017-03-01')
s_a = pd.Series(np.random.randn(60), index = dates,name='s_a')
s_b = pd.Series(np.random.randn(60), index = dates,name='s_b')
s_c = pd.Series(np.random.randn(60), index = dates,name='s_c')
s_d = pd.Series(np.random.randn(60), index = dates,name='s_d')
s_x = pd.Series(np.random.randn(60), index = dates)
df_a = pd.concat([s_a, s_b, s_c, s_d],join='outer', axis = 1)
df_a.columns = retrieve_name([s_a, s_b, s_c,s_d])
df_a.head()