假设我有一个数据帧:
df = quandl.get("FRED/DEXBZUS")
输出结果为:
print(df)
Year Value
1995-01-02 0.8440
1995-01-03 0.8450
1995-01-04 0.8450
1995-01-05 0.8430
1995-01-06 0.8400
1995-01-09 0.8440
1995-01-10 0.8470
1995-01-11 0.8510
我正在尝试创建一个由变量名称填充的新列:
print(df)
Year Value Variable
1995-01-02 0.8440 df
1995-01-03 0.8450 df
1995-01-04 0.8450 df
1995-01-05 0.8430 df
1995-01-06 0.8400 df
1995-01-09 0.8440 df
1995-01-10 0.8470 df
1995-01-11 0.8510 df
我想在循环过程中使用两个不同的数据帧来执行此操作:
df = quandl.get("FRED/DEXBZUS")
df2 = quandl.get("FRED/DEXBZUS")
data = [df, df2]
for i in data:
dps = []
for i in df:
d = i.reset_index()
d = pd.DataFrame(d)
d['variable'] = [i]
但是我没有在列中获得变量名称。
应该是这样的:
Year Value Variable
1995-01-02 0.8440 df
1995-01-03 0.8450 df
1995-01-04 0.8450 df
1995-01-05 0.8430 df
1995-01-06 0.8400 df
1995-01-09 0.8440 df
1995-01-10 0.8470 df
1995-01-11 0.8510 df
2008-01-02 0.8440 df2
2008-01-03 0.8450 df2
2008-01-04 0.8450 df2
2008-01-05 0.8430 df2
2008-01-06 0.8400 df2
2008-01-09 0.8440 df2
2008-01-10 0.8470 df2
2008-01-11 0.8510 df2
答案 0 :(得分:0)
不确定这是否是最好的方法,但它确实有效:
In [56]: df_list = []
...: for i in locals():
...: try:
...: if type(locals()[i]) == pd.core.frame.DataFrame and not i.startswith('_'):
...: df_list.append(i)
...: except KeyError:
...: pass
In [57]: df_list
Out[57]: ['df', 'df2']
In [58]: for d in df_list:
...: locals()[d]['Variable'] = d
In [59]: df
Out[59]:
Year Value Variable
0 1995-01-02 0.844 df
1 1995-01-03 0.845 df
2 1995-01-04 0.845 df
3 1995-01-05 0.843 df
4 1995-01-06 0.840 df
5 1995-01-09 0.844 df
6 1995-01-10 0.847 df
7 1995-01-11 0.851 df
In [60]: df2
Out[60]:
Year Value Variable
0 2008-01-02 0.844 df2
1 2008-01-03 0.845 df2
2 2008-01-04 0.845 df2
3 2008-01-05 0.843 df2
4 2008-01-06 0.840 df2
5 2008-01-09 0.844 df2
6 2008-01-10 0.847 df2
7 2008-01-11 0.851 df2
答案 1 :(得分:0)
要获取变量的名称,我们可以使用this answer中的代码,复制如下:
import inspect
def retrieve_name(var):
"""
Gets the name of var. Does it from the out most frame inner-wards.
:param var: variable to get name from.
:return: string
"""
for fi in reversed(inspect.stack()):
names = [var_name for var_name, var_val in fi.frame.f_locals.items() if var_val is var]
if len(names) > 0:
return names[0]
这个问题是它在循环说出列表时不会工作,因为你只需要获取局部变量的名称。这与变量名在python中的工作方式有关。变量指向一个对象,即内存中的一个位置,但内存中的位置不会指向后面。这意味着给定一个对象,你无法确定它的名字。
像列表这样的容器也是如此。例如,如果您的列表l包含两个对象a和b l=[a,b]
,则列表实际上不会保存变量a和b的名称。相反,当您创建列表时,它会记录a和b指向的内存中的位置,即对象而不是名称。
d = 'a'
print(retrieve_name(d))
#'d'
l = [d, d]
print([retrieve_name(element) for element in list ])
#['element', 'element']
话虽这么说,如果你有一个名字和对象的字典,你可以做你要求的:
name_dict = {'df': df, 'df2':df2}
dfs = [frame.assign(Variable=name) for name, frame in name_dict.items()]
combined_df = pd.concat(dfs)
但是,如果您的DataFrame实际上都有不同的数据源,那么可以更轻松地完成所有这些操作。我经常遇到这样的问题:在几个不同的源中存储数据,并且它们的名称例如是文件名。让我们说我有几个.csv文件,我正在从中读取数据,我想将它们全部合并到一个pd.DataFrame
中,但希望每行记住它来自哪个文件。
import pandas as pd
#Let's make our two fake csv files a and b:
with open('a.csv', mode='w') as a, open('b.csv', mode='w') as b:
a.write('col1,col2\n1,1')
b.write('col1,col2\n2,2')
csv_files = ['a.csv', 'b.csv']
dfs = [pd.read_csv(csv_file).assign(filename=csv_file) for csv_file in csv_files]
#assign let's you assign the value of a column and returns a DataFrame, so it's
#great for list comprehensions, in which the df['some_col']='some_var'
#syntax does not work
combined_ab = pd.concat(dfs)
combined_ab
# col1 col2 filename
#0 1 1 a.csv
#0 2 2 b.csv