基于两列对行进行分组并创建第三列,查找小于x的组并与其他组合并

2024-10-02 22:36:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含许多列的大型数据框(100000行)。以下是我的问题的相关专栏:

id   herd        birth     H_BY  HYcount      death       H_DY   HYcount2
1    1345   2005-01-09    134505       1  2010-01-09    134510       1
2    1345   2010-03-05    134510       2  2015-01-09    134515       2
3    1345   2010-05-10    134510       2  2015-01-09    134515       2
4    1345   2011-06-01    134511       1  2016-01-09    134516       1
5    1345   2012-09-01    134512       1  2017-01-09    134517       2
6    1345   2015-09-13    134515       1  2017-01-09    134517       2
7    1346   2015-10-01    134615       3  2019-01-09    134619       1
8    1346   2015-10-27    134615       3  2020-01-09    134620       2
9    1346   2015-11-10    134615       3  2020-01-09    134620       2
10   1346   2016-12-10    134616       1  2021-01-09    134621       1

我正在创建牛群年固定效果。 我已经将从众和出生/死亡列组合成从众+出生年和从众+死亡年两个单独的列,并计算了每个固定效应在数据框中出现的时间。如上所述

但是,现在我想检查整个数据帧中小于3的HYcount和HYcount2。所以我不想在HY组中有任何1或2

我想运行数据帧,并将每个组1或2的HY组组合到其他组中。低于或高于

编辑

我也只想在每个畜群中组合HY组

所以我不想将一个牧群的牧群成员添加到另一个具有牧群年份变量的牧群中

这是我用出生年份固定效应试过的

#Sort the df by the relevant value
df= df.sort_values(by=['H_BY'])


df.loc[
    (df['HYcount'] < 3),
    'H_BY'] = df['H_BY'].shift(-1)

#Count the values again 
df['HC1_c'] = df.groupby('H_BY')['H_BY'].transform('count')

但这是一个非常微弱的尝试。我必须多次执行此操作,以清除数据帧中所有小于3的值,并且它无法处理记录编号1。我想在至少4列上重复这个过程

编辑

当然,这段代码对在一个群体中组合没有任何作用

有什么建议和窍门或想法,我可以如何更有效地做到这一点

编辑

上述数据除外

id   herd        birth     H_BY  HYcount      death       H_DY   HYcount2
1    1345   2005-01-09    134510       3  2010-01-09    134515       3
2    1345   2010-03-05    134510       3  2015-01-09    134515       3
3    1345   2010-05-10    134510       3  2015-01-09    134515       3
4    1345   2011-06-01    134515       3  2016-01-09    134517       3
5    1345   2012-09-01    134515       3  2017-01-09    134517       3
6    1345   2015-09-13    134515       3  2017-01-09    134517       3

7    1346   2015-10-01    134615       4  2019-01-09    134620       4
8    1346   2015-10-27    134615       4  2020-01-09    134620       4
9    1346   2015-11-10    134615       4  2020-01-09    134620       4
10   1346   2016-12-10    134615       4  2021-01-09    134620       4

Tags: the数据id编辑dfby效应birth
1条回答
网友
1楼 · 发布于 2024-10-02 22:36:05

为了解决这个问题,我删除了H_BYH_DY列,以便允许对组进行动态计数。在数据帧中包含计数的问题之一是,如前所述,您需要重新计算它 更改分组的时间,以及重复计数的时间

然后,我将birthdeath更改为datetimes,以便为出生年份和死亡年份bydy创建新列

ff = df[[ 'herd', 'birth', 'death' ]].copy()

ff[ 'birth' ] = pd.to_datetime( ff[ 'birth' ] )
ff[ 'death' ] = pd.to_datetime( ff[ 'death' ] )
ff = ff.assign( 
    by = lambda x: x.birth.apply( lambda y: y.year ),
    dy = lambda x: x.death.apply( lambda y: y.year )
)
^{tb1}$

对于实际处理,我们首先按herd分组,这样就不会在它们之间混淆。然后,如果可能,我们向前合并组,否则向后合并组,直到不再发生合并。最后,我们将这些组分配回原始数据

tdf = []
for herd, data in ff.groupby( 'herd' ):
    # get counts and assign initial groups
    counts = data[ 'by' ].value_counts().sort_index().to_frame()
    counts[ 'group' ] = range( counts.shape[ 0 ] )
    
    while True:
        gcounts = counts.groupby( 'group' ).sum()[ 'by' ]  # group counts
        change = gcounts[ gcounts.values < 3 ]  # groups with too few
        
        if change.shape[ 0 ] == 0:
            # no changes, exit
            break

        # check how to merge groups
        cgroup = change.index.min()
        groups = gcounts.index.values
        g_ind = list( groups ).index( cgroup )
        if ( g_ind + 1 ) < groups.shape[ 0 ]:
            # merge forward
            ngroup = groups[ g_ind + 1 ]
            
        elif g_ind > 0:
            # merge backward
            ngroup = groups[ g_ind - 1 ]
            
        else:
            # no groups to merge
            print( f'Can not merge herd {herd}' )
            break

        counts.loc[ counts[ 'group' ] == cgroup, 'group' ] = ngroup 

    # assign groups
    for ind, gdata in counts.iterrows():
        data.loc[ data[ 'by' ] == ind, 'group' ] = gdata[ 'group' ]
        
    tdf.append( data )
    
tdf = pd.concat( tdf )
^{tb2}$

最后,如果仍然需要用于分组的H_BY标识符,可以使用

tdf[ 'H_BY' ] = tdf[ 'herd' ].astype( 'str' ) + tdf[ 'group' ].astype( int ).astype( str )

相关问题 更多 >