[英]How to multiply or divide two Series in python?
I have a dataset like this.我有一个这样的数据集。 The actual dataset is much larger though.
但实际数据集要大得多。
data1 = pd.DataFrame({'Name':["Tom","Andy","Joseph","Joe","Mary","Alexa","Chris","Jessica","Jimmy","Andrea","George","Bruce","Will","Eric","Leonard","Ryan","Megan","Michael","Sara"],\
"City":["NY","DC","LAX","NY","DC","DC","SF","SD","NY","SF","SD","DC","LAX","SF","LAX","NY","SF","PDX","FL"],\
'Car':["Ford","Ford","TOYOTA","GM","GM","Honda","GM","Porsche","Tesla","TOYOTA","Tesla","Tesla","Honda","GM","Nissan","Porsche","Nissan","Ford","Tesla"]})
First, I want to calculate the actual frequency of the "City" and "Car" combination and did this.首先,我想计算“城市”和“汽车”组合的实际频率并这样做。
df_City_Car_actual=data1.groupby(["City","Car"]).size()
df_City_Car_actual
Then I want to calculate the expected frequency of the "City" and "Car" combination.然后我想计算“城市”和“汽车”组合的预期频率。 So I did this first.
所以我先做了这个。
df_City=data1.groupby("City").size()
df_City
df_Car=data1.groupby("Car").size()
df_Car
Then I want to multiply df_City and df_Car and show the expected frequency of City x Car.然后我想乘以 df_City 和 df_Car 并显示 City x Car 的预期频率。 For example, "DC" frequency is 4 in the df_City and "Ford" frequency is 3 in the df_Car.
例如,df_City 中的“DC”频率为 4,df_Car 中的“Ford”频率为 3。 Therefore, the DC x Ford expected frequency will be 4x3=12.
因此,DC x Ford 预期频率将为 4x3=12。
I tried this but did not work我试过这个但没有用
df_City_Car_expected=df_City*df_Car
df_City_Car_expected
Finally, I want to divide the df_City_Car_actual by the df_City_Car_expected so that the final data will be normalized.最后,我想将 df_City_Car_actual 除以 df_City_Car_expected,以便对最终数据进行归一化。 Is there a good way to do this?
有没有好的方法可以做到这一点? Thanks for your help.
谢谢你的帮助。
The simplest way I can think of is by using the numpy
"outer product" function, as such:我能想到的最简单的方法是使用
numpy
“外积”函数,如下所示:
pd.DataFrame(np.outer(df_City.values, df_Car.values), index=df_City.index, columns=df_Car.index)
Which gives:这使:
Car Ford GM Honda Nissan Porsche TOYOTA Tesla
City
DC 12 16 8 8 8 8 16
FL 3 4 2 2 2 2 4
LAX 9 12 6 6 6 6 12
NY 12 16 8 8 8 8 16
PDX 3 4 2 2 2 2 4
SD 6 8 4 4 4 4 8
SF 12 16 8 8 8 8 16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.