简体   繁体   English

合并数据框架不同的行和不同的列

[英]Merging data frame different rows and different columns

I have a data frame like this 我有这样的数据框

df1 DF1

  a       b       c

  10      11    10
  00021   11    00021
  022     1     00021
  00054   32    00054

and another one like this 和另一个这样的人

df1 DF1

  name     n

  10       1
  00021    2
  022      1
  00054    1

that counts how many times the variables in the column c of df1 appear. 计算df1c中变量的出现次数。 I would like to add to the first data frame one column like n in df2 ; 我想在df2添加第一个数据帧,如n ; I tried to merge the two data frame but without success.. 我试图合并两个数据框但没有成功..

You can use merge : 您可以使用merge

merge(df1, df2, by.x = "c", by.y = "name")

      c     a  b n
1 00021 00021 11 2
2 00021   022  1 2
3 00054 00054 32 1
4    10    10 11 1

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

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