I have the following df
dictionary1 = {'trade_date': {1350: 20151201,
6175: 20151201,
3100: 20151201,
5650: 20151201,
3575: 20151201,
1: 20170301,
2: 20170301},
'comId': {1350: '257762',
6175: '1038328',
3100: '315476',
5650: '658776',
3575: '329376',
1: '123456',
2: '987654'},
'return': {1350: -0.0018,
6175: 0.0023,
3100: -0.0413,
5650: 0.1266,
3575: 0.0221,
1: 0.9,
2: 0.01}}
df1 = pd.DataFrame(dictionary1)
I want to create a new column called weights
and assign equal weights based on the number of entries for a particular trade_date
.
In other words, the output should be like this:
dictionary2 = {'trade_date': {1350: 20151201,
6175: 20151201,
3100: 20151201,
5650: 20151201,
3575: 20151201,
1: 20170301,
2: 20170301},
'comId': {1350: '257762',
6175: '1038328',
3100: '315476',
5650: '658776',
3575: '329376',
1: '123456',
2: '987654'},
'return': {1350: -0.0018,
6175: 0.0023,
3100: -0.0413,
5650: 0.1266,
3575: 0.0221,
1: 0.9,
2: 0.01},
'weights':{1350: 0.2,
6175: 0.2,
3100: 0.2,
5650: 0.2,
3575: 0.2,
1: 0.5,
2: 0.5}}
df2 = pd.DataFrame(dictionary2)
This there are five values associated with trade_date
20151201, hence they all get a value 0.2 (1/5) in the new column weights
and 2 values associated with trade_date
20170301, hence they all get the value 0.5 (1/2) in the new column weights
.
Any suggestions how to do this?
df['weights'] = df.groupby('trade_date')['trade_date'].transform(lambda x: 1/len(x))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.