1

我在表单的数据框中有一些医院就诊医疗保健数据:

记录ID client_id date_of_encounter 医院 ID
1 MK456 2014-01-01 01J
2 JJ103 2016-04-01 02J
3 MK456 2014-02-26 01J
4 JJ103 2016-05-01 02H
5 MK456 2014-03-01 02H
6 JJ103 2016-06-06 02J

我想创建一个列,该列hospital_count是每个客户在date_of_counter. 我已经按client_idand排序了
date_of_counter。结果转换将是

记录ID client_id date_of_encounter 医院 ID 医院计数
1 MK456 2014-01-01 01J 1
3 MK456 2014-02-26 01J 1
5 MK456 2014-03-01 02H 2
2 JJ103 2016-04-01 02J 1
4 JJ103 2016-05-01 02H 2
6 JJ103 2016-06-06 02J 2

有些人建议使用 agroupby和的组合,cumsum()但我不太确定如何?

4

1 回答 1

0

使用GoupBy.cumcount

每个客户就诊的不同医院数量的累计计数

import pandas as pd

df = pd.DataFrame({
  'record_id': list(range(1,7)),
  'client_id':['MK', 'JJ', 'MK', 'JJ', 'MK', 'JJ'],
  'date': [20140101, 20160401,20140226,20160501,20140301,20160606],
  'hospital': ['1j', '2j', '1j', '2h', '2h', '2j']
})

df.sort_values(by=['client_id', 'date'], inplace=True)

df['hospital_count'] = df.drop_duplicates(subset=['client_id', 'hospital']
  ).groupby('client_id').cumcount() + 1

df.fillna(method='ffill', inplace=True)

print(df)
#    record_id client_id      date hospital  hospital_count
# 1          2        JJ  20160401       2j             1.0
# 3          4        JJ  20160501       2h             2.0
# 5          6        JJ  20160606       2j             2.0
# 0          1        MK  20140101       1j             1.0
# 2          3        MK  20140226       1j             1.0
# 4          5        MK  20140301       2h             2.0

解释:我们使用 ; 删除同一客户到同一家医院的连续访问drop_duplicates;然后我们可以简单地使用groupby和来计算每个客户的访问量cumcount。但是,这会在被删除的行中留下NaN值;我们使用 填充这些值fillna

每位客户到每家医院的累计就诊次数

import pandas as pd

df = pd.DataFrame({
  'record_id': list(range(1,7)),
  'client_id':['MK', 'JJ', 'MK', 'JJ', 'MK', 'JJ'],
  'date': [20140101, 20160401,20140226,20160501,20140301,20160606],
  'hospital': ['1j', '2j', '1j', '2h', '2h', '2j']
})

df['hospital_count'] = df.sort_values(by=['client_id', 'hospital', 'date']
  ).groupby(['client_id', 'hospital']
  ).cumcount() + 1

print(df)
#    record_id client_id      date hospital  hospital_count
# 0          1        MK  20140101       1j               1
# 1          2        JJ  20160401       2j               1
# 2          3        MK  20140226       1j               2
# 3          4        JJ  20160501       2h               1
# 4          5        MK  20140301       2h               1
# 5          6        JJ  20160606       2j               2
于 2021-08-12T14:39:31.397 回答