计算大熊猫中的每个单元格

时间:2020-02-01 06:17:05

标签: python pandas

在遍历此循环时,我希望对行中的每个单元格应用计算:

#count how many times the name exists
counts = df['name'].value_counts()


for i, max_val in enumerate(df.groupby(['name'], sort=True)['values'].max()):
        count = counts[i]
        x = 0
        while x <= count:
#I know I am doing something wrong here but not sure how to get each cell in a newly created column
            df['value%'] = df['values']/max_val
            x += 1

2 个答案:

答案 0 :(得分:1)

这是一个愚蠢的方式

sample_data = {'name':['Jim','Sam','Frank','Sam'],
               'values':[1,4,7,8]}

df = pd.DataFrame.from_dict(sample_data)

df_gpd = df.groupby(['name'],sort=True)['values'].max()
maxdict = df_gpd.to_dict()

def value_pcg(x):
    return x['values']/maxdict[x['name']]

df['value_pcg'] = df.apply(lambda x: value_pcg(x), axis=1)

print(df)

  name  values  maxs  value_pcg
0    Jim       1     1        1.0
1    Sam       4     8        0.5
2  Frank       7     7        1.0
3    Sam       8     8        1.0

答案 1 :(得分:1)

IIUC:

df["value%"]=df["values"].div(
    df.groupby("name")["values"].max().loc[df["name"]].reset_index(drop=True)
)