简体   繁体   English

使用循环获取 pandas 中每个 dataframe 条目的计算值总和(使用列数学)

[英]Using a loop to get a sum of computed values (using column math) for each dataframe entry in pandas

I have the following data frame:我有以下数据框:

在此处输入图像描述

For each entry of this data frame, I am looking to get the sum of log(df['s_i']/df['s_18'])*log(i/18) for i in h = [18, 56, 98, 123, 148].对于这个数据帧的每个条目,我希望得到 log(df['s_i']/df['s_18'])*log(i/18) for i in h = [18, 56, 98 , 123, 148]。

I tried the following:我尝试了以下方法:

a = []
h = [18, 56, 98, 123, 148]

for i in df.index:
  for zi in h:
    a.append(log(df.loc[i,'s_'+str(zi)]/df.loc[i,'s_18'])*log(zi/18))
    b = sum(a)

However, it did not work.但是,它没有用。 Any idea how to solve this problem?知道如何解决这个问题吗? Any help would be greatly appreciated!任何帮助将不胜感激!

Here one thing to note use np.log<i> or math.log where is <i> base what ever you want if you don't have any custom implementation log function.如果您没有任何自定义实现log function,这里需要注意的一件事是使用np.log<i> or math.log where is <i> base what ever you want基础。
zr should to change to 18 as per your statement formula zr应该根据您的语句公式更改为18

log(df['s_i']/df['s_18'])*log(i/18) 

Here i am using similar layout dataframe , please check out this code:这里我使用类似的布局dataframe请查看此代码:

import numpy as np
import pandas as pd

h = [18, 56, 98, 123, 148]

df = pd.DataFrame(
    data= np.arange(1, 51).reshape(10, 5),
    columns= ['s_'+str(i) for i in h]
)

# here one point should be noted that `b += sum(a)` should be in outer `for-loop` 
# otherwise as `a` grows at each iteration its `all items again added` so put
# `b += sum(a) in outer for-loop`

b = 0
for i in df.index:
    a = []
    for zi in h:
        a.append(np.log(df.loc[i,'s_'+str(zi)]/df.loc[i,'s_18'])*np.log(zi/18))
    b += sum(a)

print(b)

Method-2方法二

equation = "np.log(df.loc[i,'s_'+str(j)]/df.loc[i,'s_18'])*np.log(j/18)"
b = 0
for i in df.index:
    b += sum(list(map(lambda j: eval(equation), h)))

print(b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用居中的.rolling()和第一个计算得出的和之后,替换Pandas DataFrame列中的NaN值 - Replace NaN values in a Pandas DataFrame column after using centered .rolling() with first computed sum 使用 for 循环替换 Pandas 中每一行和每一列的单元格值 - Replace cell values for each row and each column in Pandas using for loop 如何在Python中使用for循环将值保存在pandas dataframe的列中 - how to save values in a column of pandas dataframe using for loop in python 使用Pandas数据框循环查看最小/最大列值? - Loop for min/max column values using pandas dataframe? 使用先前计算的值在pandas DataFrame中创建一列 - Create a column in a pandas DataFrame using the previously computed value 使用 for 循环替换 pandas 列的每一行中的单元格值 - Replace cell values in each row of pandas column using for loop 使用binarizer和for循环替换pandas列每一行中的单元格值 - Replace cell values in each row of pandas column using for binarizer and for loop 如何使用熊猫数据框扩展求和列 - how to expand sum column using pandas dataframe 如何使用非唯一列将具有求和值的熊猫Groupby数据框映射到另一个数据框 - How to map pandas Groupby dataframe with sum values to another dataframe using non-unique column 使用列值从Pandas DataFrame获取数据 - Get data from Pandas DataFrame using column values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM