我有2个数据帧(现在我说2个,但我们可以有n个数据帧)。数据框的名称在列表中。我想加入名称出现在列表中的所有数据框
import pandas as pd
data1 = [['Alex',10],['Bob',12],['Clarke',13]]
df1 = pd.DataFrame(data1,columns=['Name','Age'])
data2 = [['David',10],['Alain',12],['Rob',13]]
df2 = pd.DataFrame(data2,columns=['Name','Age'])
list = ['df1','df2']
# i want something to work like
df3 = pd.Concat(list)
感谢您的帮助。
答案 0 :(得分:1)
您需要将名称设置为索引,然后执行<script>
function moreButton() {
$('.more').click(function(){
var id=$(this).data("id");
$.ajax({
url:"more.php",
data:{id:id},
method:"post",
success:function(data){
if(data!=''){
$('#load_data_table').append(data);
$('#remove_row').remove();
moreButton();
}
else{
$('.more:last').html("No Data");
}
}
});
}
$(document).ready(function(){
moreButton();
});
concat
或使用list = [df1,df2]
pd.concat([x.set_index('Name') for x in list],axis=1)
Out[270]:
Age Age
Alain NaN 12.0
Alex 10.0 NaN
Bob 12.0 NaN
Clarke 13.0 NaN
David NaN 10.0
Rob NaN 13.0
+ reduce
merge
答案 1 :(得分:0)
如果您想concat,则使用以下结果:
pd.concat([df1, df2], ignore_index=True)
# Name Age
#0 Alex 10
#1 Bob 12
#2 Clarke 13
#3 David 10
#4 Alain 12
#5 Rob 13
答案 2 :(得分:0)
请勿将变量名用作list
。遍历列表以获取数据帧的实例,然后进行串联
lis = ['df1','df2']
df3 = pd.concat([globals()[i] for i in lis]) # to get instance of df1 from string 'df1'
df3
输出
Name Age
Alex 10
Bob 12
Clarke 13
David 10
Alain 12
Rob 13