2 回答
TA贡献1719条经验 获得超6个赞
我无法访问 Zero_one_frequencies df。所以我冒昧地尝试用我的方式解决这个问题。
import pandas as pd
import numpy as np
col1 = ['a','b','c','a','c','a','b','c','a']
col2 = [1,1,0,1,1,0,1,1,0]
df2 = pd.DataFrame(zip(col1,col2),columns=['name','count'])
df2["name_0"] = 0
df2["name_1"] = 0
for name in df2['name'].unique():
df_name = df2[df2['name'] == name]
prob_1 = sum(df_name['count']/df_name.shape[0])
for count in df2['count'].unique():
indx = np.where((df2['name'] == name) & (df2['count'] == count))
df2["name_" + str(count)].loc[indx] = np.abs(((count +1) % 2) - prob_1)
输出:
name count name_0 name_1
0 a 1 0.000000 0.500000
1 b 1 0.000000 1.000000
2 c 0 0.333333 0.000000
3 a 1 0.000000 0.500000
4 c 1 0.000000 0.666667
5 a 0 0.500000 0.000000
6 b 1 0.000000 1.000000
7 c 1 0.000000 0.666667
8 a 0 0.500000 0.000000
TA贡献1773条经验 获得超3个赞
以下代码解决了该问题。但是,我找不到使用 numpy.select 获得相同效果的方法。
df2["name"+str("_0")] = 0.0
df2["name"+str("_1")] = 0.0
for j in df2.name.unique():
print(j)
zero_ct = zero_one_frequencies[zero_one_frequencies['name'] == j][0]
full_ct = zero_one_frequencies[zero_one_frequencies['name'] == j][0] + zero_one_frequencies[zero_one_frequencies['name'] == j][1]
zero_pb = zero_ct / full_ct
one_pb = 1 - zero_pb
print(f"ZERO Probablitliy for {j} = {zero_pb.tolist()[0]}")
print(f"One Probablitliy for {j} = {one_pb.tolist()[0]}")
print("="*30)
for idx in df2[df2['name']== j ].index:
print("Index:::", idx)
if df2['count'].iloc[idx] == 0:
df2.at[idx, "name"+str("_0")] = zero_pb.tolist()[0]
print(f'Count for {j} at index {idx} is {a}')
print('printing name_0: ', df2["name"+str("_0")].iloc[idx])
print("*"*30)
elif df2['count'].iloc[idx] == 1:
df2.at[idx, "name"+str("_1")] = one_pb.tolist()[0]
print(f'Count for {j} at index {idx} is {b}')
print('printing name_1: ', df2["name"+str("_1")].iloc[idx])
print("*"*30)
添加回答
举报