大熊猫:基于另一个数据帧中行值的计数进行转换

时间:2018-11-26 20:29:54

标签: python pandas

我有两个数据框:

df1:

Gender  Registered
female  1
male    0
female  0
female  1
male    1
male    0

df2:

Gender
female
female
male
male

我想修改df2,以便为df1中相应的性别值添加一个新列“ Count”,其注册计数为1。例如,在df1中,有2个注册女性和1个注册男性。我要转换df2,以便输出如下:

输出:

Gender  Count
female  2
female  2
male    1
male    1

我尝试了很多事情并且接近了,但无法完全发挥作用。

2 个答案:

答案 0 :(得分:3)

Nov 26 06:52:04 01 appserver.linux[6550]: %4|1543215124.447|REQTMOUT|rdkafka#producer-1| [thrd:kafka-broker-3:9092/bootstrap]: kafka-broker-3:9092/bootstrap: Timed out 0 in-flight, 1 retry-queued, 0 out-queue, 0 partially-sent requests Nov 26 06:52:10 01 appserver.linux[6550]: %4|1543215130.448|REQTMOUT|rdkafka#producer-1| [thrd:kafka-broker-3:9092/bootstrap]: kafka-broker-3:9092/bootstrap: Timed out 0 in-flight, 1 retry-queued, 0 out-queue, 0 partially-sent requests Nov 26 08:46:57 01 appserver.linux[6550]: 2018/11/26 08:46:57 Ignored event: kafka-broker-2:9092/bootstrap: Disconnected (after 600000ms in state UP) Nov 26 08:47:02 01 appserver.linux[6550]: %4|1543222022.803|REQTMOUT|rdkafka#producer-1| [thrd:kafka-broker-2:9092/bootstrap]: kafka-broker-2:9092/bootstrap: Timed out 0 in-flight, 1 retry-queued, 0 out-queue, 0 partially-sent requests Nov 26 08:47:09 01 appserver.linux[6550]: %4|1543222029.807|REQTMOUT|rdkafka#producer-1| [thrd:kafka-broker-2:9092/bootstrap]: kafka-broker-2:9092/bootstrap: Timed out 0 in-flight, 1 retry-queued, 0 out-queue, 0 partially-sent requests + sum

map

v = df1.groupby('Gender').Registered.sum()
df2.assign(Count=df2.Gender.map(v))

答案 1 :(得分:2)

pd.merge

pd.merge(df2, df1.groupby('Gender', as_index=False).sum())

    Gender  Registered
0   female  2
1   female  2
2   male    1
3   male    1