【问题标题】:Excel data calculation using Python使用 Python 计算 Excel 数据
【发布时间】:2021-10-11 15:41:06
【问题描述】:

my excel data

嗨,我是 Python 新手。我需要使用Python对excel数据进行算术计算,但是数据排列有点复杂。

我要计算

(D5*D4 + C5*C4)*B5

结果放在H列

然后它继续到整个材料(脂肪组织新生儿#1 到脂肪组织成人#1)

(D6*D4 + C6*C4)*B6
(D7*D4 + C7*C4)*B7
(D8*D4 + C8*C4)*B8

等等

谁能帮帮我?以下是我当前的代码,我被困在如何编写操作代码中。提前非常感谢您

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
%matplotlib inline

# read an excel file and convert 
# into a dataframe object
df = pd.DataFrame(pd.read_excel("ICRU_blank_af.xlsx"))
# show the dataframe
df.head(5)

【问题讨论】:

  • 有什么原因不能在 Excel 中进行此计算吗?看起来您只需要在行中添加一些绝对引用; IE。 =(D5*D$4 + C5*C$4)*B5 在单元格 H5 中并复制下来。

标签: excel excel-formula


【解决方案1】:

我会推荐如下内容:

    data = {'first_column':  [1, 2, 3, 4, 5, 6, 7, 8],
        'second_column': [1, 2, 3, 4, 5, 6, 7, 8],
        'third_column':  [1, 2, 3, 4, 5, 6, 7, 8],
        'fourth_column': [1, 2, 3, 4, 5, 6, 7, 8]
        }

df = pd.DataFrame(data)

#solving for ((D*D)+(C*C))*B
#Assume first_column equates to Col D
#Assume second_column equates to Col C
#Assume third_column equates to Col B

#Pulling pairs of D column
first_pair = list(zip(df['first_column'], df['first_column'].iloc[1:]))

#pulling pairs of C column
second_pair = list(zip(df['second_column'], df['second_column'].iloc[1:]))

#(D5*D4)
first_multiplication = [reduce(operator.mul, tup, 1) for tup in first_pair]

#(C5*C4)
second_multiplication = [reduce(operator.mul, tup, 1) for tup in second_pair]

final_value = []

#solving for ((D5*D4)+(C5*C4))*B5 
for first, second, third in zip(first_multiplication,second_multiplication,df['third_column']):
    value = first + second *third
    final_value.append(value)

我的结果如下: first_pair/second_pair = [(1,2),(2,3),(3,4),(4,5)...]

first_multiplication/second_multiplication = [2,6,12,20,...]

第一个公式值 = 4 ((2)+(2))*1 最终列表(附加到新列)= [4,24,72,160,300,504,784]

【讨论】:

  • 您好,谢谢您的回答,但是我的表格有点复杂。我认为您提供的代码是针对基本表格的,但我的表格和所需的计算很复杂。我需要在我的计算中“拉”一些特定的单元格,所以这是我的困惑
  • @HanaT.A 我编辑了我的回复。请注意,我基本上已经给了你公式。如果您有任何问题,请告诉我。如果我解决了您的问题,请也支持我的解决方案。
【解决方案2】:

我更详细地阅读了您的问题,并且我认为您正在考虑创建一些常量:

data = {'first_column':  [1, 2, 3, 4, 5, 6, 7, 8],
        'second_column': [1, 2, 3, 4, 5, 6, 7, 8],
        'third_column':  [1, 2, 3, 4, 5, 6, 7, 8],
        'fourth_column': [1, 2, 3, 4, 5, 6, 7, 8]
        }

df = pd.DataFrame(data)

#solving for ((D5*$D$4)+(C5*$C$4))*B5
#Assume first_column equates to Col d
#Assume second_column equates to Col C
#Assume third_column equates to Col B

first_pairing = []
second_pairing = []

#(D*$D$X)
for cell in df['first_column']:
    pairing = cell * df['first_column'].iloc[0]
    first_pairing.append(pairing)

#(C*$C$X)    
for cell in df['second_column']:
    pairing = cell * df['second_column'].iloc[0]
    second_pairing.append(pairing)
    
final_value = []

#solving for ((D5*$D$4)+(C5*$C$4))*B5 
for first, second, third in zip(first_pairing,second_pairing,df['third_column']):
    value = first + second *third
    final_value.append(value)

我会将 iloc 值替换为您希望保留为常量的确切行,以及与 df 标题名称匹配的列名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多