连续加入两个具有相同列名的数据框?

时间:2018-09-26 05:17:11

标签: python pandas dataframe

输入:

df = pd.DataFrame({"A":["foo", "foo", "foo", "bar"],"B":[0,1,1,1]})


   df1 = pd.DataFrame({"A":["Panda", "Panda", "Zootopia", "Zootopia"],"B":[0,1,1,1]})

输出:

     A          B
0   foo         0
1   foo         1
2   foo         1
3   bar         1
4   Panda       0
5   Panda       1
6   Zootopia    1
7   Zootopia    1

像预期的输出一样加入df和df1。

3 个答案:

答案 0 :(得分:2)

# function to get the sum of two columns from 'people' get_sum <- function(m) { members <- unlist(strsplit(m, ' + ', fixed = T)) rowSums(people[, members]) } # "Sam + Robert" get_sum(teams$members[1]) # [1] 1015 784 1606 742 # apply this for every team, and add results to 'people' cbind(people, sapply(teams$members, get_sum)) # region Sam Frank Dennis Steven Robert Georgia Sam + Robert Frank + Georgia Frank + Steven Robert + Dennis Frank + Sam # 1 Region_1 218 763 811 812 797 574 1015 1337 1575 1608 981 # 2 Region_2 474 983 343 697 310 335 784 1318 1680 653 1457 # 3 Region_3 700 813 133 212 906 680 1606 1493 1025 1039 1513 # 4 Region_4 212 581 893 514 530 795 742 1376 1095 1423 793 呢?

DataFrame.append

答案 1 :(得分:2)

使用concat和参数ignore_index=True来避免重复的索引值:

df = pd.concat([df,df1], ignore_index=True) 
print (df)

          A  B
0       foo  0
1       foo  1
2       foo  1
3       bar  1
4     Panda  0
5     Panda  1
6  Zootopia  1
7  Zootopia  1

答案 2 :(得分:1)

或用reset_index代替concat

print(pd.concat([df,df1]).reset_index(drop=True))