Python 将数据帧中的特定列(包含)除以另一个数据帧

Python 将数据帧中的特定列(包含)除以另一个数据帧,python,pandas,Python,Pandas,我有一个大的数据集,我发现概率。虽然有很多专栏,但我只有两个感兴趣的动物和颜色。我想数一数动物出现的概率并打印颜色 animal weight color dog 10 white dog 11 white cat 18 white cat 15 black bird 16 white bird 11 black bird 10 white

我有一个大的数据集,我发现概率。虽然有很多专栏,但我只有两个感兴趣的动物和颜色。我想数一数动物出现的概率并打印颜色

animal   weight   color
dog      10       white
dog      11       white
cat      18       white
cat      15       black
bird     16       white
bird     11       black
bird     10       white
df=pd.read_csv('test.csv')
animal_color=df.groupby('animal').aspiration.value_counts().loc[:,['white','black']]
color=df.animal.value_counts()
我预计会有以下结果:

prob(animal=dog|color=white) = 100.0%
prob(animal=dog|color=black) = 0.00%
prob(animal=cat|color=white) = 50.0%
prob(animal=cat|color=black) = 50.0%
prob(animal=bird|color=white) = 66.67%
prob(animal=bird|color=black) = 33.33%
这里有两种方法

df.groupby(['animal']).color.value_counts(normalize=True)
animal  color
bird    white    0.666667
        black    0.333333
cat     black    0.500000
        white    0.500000
dog     white    1.000000
Name: color, dtype: float64

pd.crosstab(df.animal,df.color,normalize='index')
color      black     white
animal                    
bird    0.333333  0.666667
cat     0.500000  0.500000
dog     0.000000  1.000000

谢谢你的帮助。如何在每行的前面打印“prob”?我还希望在每一行上打印动物名称,即bird,白色,0.66667和bird,黑色,0.33333。@user11861166
pd.crosstab(df.animal,df.color,normalize='index').stack().reset_index()