我想在不列出concat函数中所有名称的情况下,将许多DataFrame在一起。我已经以字符串元素列表的形式获得了数据框的名称列表。无论如何,我可以将其传递给concat函数吗?
df_1 = pandas.read_csv('some_file_1')
df_2 = pandas.read_csv('some_file_2')
df_3 = pandas.read_csv('some_file_3')
some_list = ['df_1','df_2','df_3']
pandas.concat(some_list)
答案 0 :(得分:1)
您可以这样做:
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df2 = pd.DataFrame(np.random.randint(0,50,size=(100, 4)), columns=list('ABCD'))
df3 = pd.DataFrame(np.random.randint(0,5,size=(100, 4)), columns=list('ABCD'))
to_concat = [ "df1", "df2", "df3" ]
df = pd.concat(map(lambda z: eval(z),to_concat))
print(df)