我想创建一个新列,它是每个 TimePeriodId 的“BillType”列中“IN”和“SA”的生命总和。这样,我将为单个 TimePeriodId 设置一个“总生命数”条目。我已经浏览了很多文档,但无法弄清楚在这种情况下我会怎么做。
代码示例:
sa = pd.read_sql(sa_q1, sql_conn)
#convert TimePeriodId to string values
sa['TimePeriodId'] = sa['TimePeriodId'].astype(str)
sa = sa.loc[(sa['BillType'] =='SA') | (sa['BillType']=='IN')]#.drop(['BillType'], axis = 1)
sa.head(10).to_dict()
#the last line returns the following:
{'TimePeriodId': {1: '201811',
2: '201811',
4: '201812',
5: '201812',
9: '201901',
11: '201901',
13: '201902',
14: '201902',
17: '201903',
18: '201903'},
'BillType': {1: 'IN',
2: 'SA',
4: 'IN',
5: 'SA',
9: 'SA',
11: 'IN',
13: 'IN',
14: 'SA',
17: 'IN',
18: 'SA'},
'Lives': {1: 1067,
2: 288028,
4: 1058,
5: 287501,
9: 293560,
11: 1068,
13: 1089,
14: 278850,
17: 1076,
18: 276961}}
任何帮助,将不胜感激!
GCT1015
相关分类