简体   繁体   English

如何在python中附加具有相同前缀的多个数据帧

[英]How to append multiple dataframes with same prefix in python

I have multiple sequential dataframe like this: 我有这样的多个顺序数据帧:

df1 = pd.DataFrame( [['tom', 10], ['nick', 15], ['juli', 14]] ,  columns = ['Name', 'Age'])
df2 = pd.DataFrame([['tom', 10], ['nick', 15], ['juli', 14]] ,  columns = ['Name', 'Age'])
df3 = pd.DataFrame([['tom', 10], ['nick', 15], ['juli', 14]] ,  columns = ['Name', 'Age'])
df4 = pd.DataFrame([['tom', 10], ['nick', 15], ['juli', 14]] ,  columns = ['Name', 'Age'])

I need to create a for loop to append them and get a new dataframe. 我需要创建一个for循环来追加它们并获得一个新的数据帧。 I tried the codes below, but it doesnt work, as python recognise df1 as a string. 我尝试了下面的代码,但它不起作用,因为python将df1识别为字符串。

tempdf = df1
for i in range(2,4):
     tempdf = tempdf.append(("df"+str(i)))
     print(tempdf)

How do I get python to recognise them as dataframe objects I created? 如何让python将它们识别为我创建的数据框对象?

First, I should highlight that having to do this suggests a problem in the way the source dataframes were generated, and you should look into fixing that. 首先,我应该强调,必须这样做表明源数据帧的生成方式存在问题,您应该考虑修复它。

With Python, there are ways to do almost anything you want. 使用Python,有几种方法可以做任何你想要的事情。 Whether it is desirable to make use of such power is another question altogether. 是否希望利用这种能力是另一个问题。

In this case, the safest way would probably be to use globals() : 在这种情况下,最安全的方法可能是使用globals()

n_dataframes = 4
g = globals()
dataframes = [g[f'df{i}'] for i in range(1, n_dataframes + 1)]

result_df = pd.concat(dataframes)    
print(result_df)

Output: 输出:

   Name  Age
0   tom   10
1  nick   15
2  juli   14
0   tom   10
1  nick   15
2  juli   14
0   tom   10
1  nick   15
2  juli   14
0   tom   10
1  nick   15
2  juli   14

You can perform further processing on the result, such as calling reset_index . 您可以对结果执行进一步处理,例如调用reset_index

Another alternative is to use eval , which veers firmly into "you shouldn't do this unless you really know what you're doing" territory, because it allows execution of arbitrary code: 另一个选择是使用eval ,它会坚定地转向“你不应该这样做,除非你真的知道你正在做什么”的领域,因为它允许执行任意代码:

dataframes = [eval(f'df{i}') for i in range(1, n_dataframes + 1)]

Note that the above code uses f-strings , which are syntax introduced only in Python 3.6. 请注意,上面的代码使用f-strings ,这是仅在Python 3.6中引入的语法。 Accordingly, if your Python version is below that, replace f'df{i}' with 'df{}'.format(i) . 因此,如果您的Python版本低于该版本,请将f'df{i}'替换为'df{}'.format(i)

You were proceeding in correct direction, just use eval : 你正在朝着正确的方向前进,只需使用eval

tempdf = df1
for i in range(2,4):
     tempdf = tempdf.append(eval("df"+str(i)))
     print(tempdf)

Note: Using eval can run arbitrary code, using it is considered a bad practice. 注意:使用eval可以运行任意代码,使用它被认为是一种不好的做法。 Please try to use other ways, if possible. 如果可能,请尝试使用其他方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM