如何计算 excel 中条件单元格的总和,用结果填充另一列

How to calculate the sum of conditional cells in excel, populate another column with results

编辑:使用 Excel 中的高级搜索(在数据选项卡下)我已经能够创建唯一公司名称的列表,现在我能够根据包含公司名称的单元格进行 SUMIF!

免责声明:任何 python 解决方案也将不胜感激,特别是 pandas!

我有 60,000 行数据,其中包含有关授予公司的赠款的信息。

我计划创建一个 python 字典来存储每个唯一的公司名称,以及给定的总资助 $ (agreemen_2) 和位置坐标。然后,我想在加拿大的实时 MapBox 地图上使用 Dash (Plotly) 显示它。

首先,我如何计算和存储授予每家公司的总价值?

我在其他解决方案中看到过 SUMIF,但不确定如何将其输出到新列(如果这有意义的话)。

我认为一个可能的解决方案是创建一个新的唯一公司名称列,并在其旁边 SUMIF 列 D 中的所有适当单元格。

PYTHON 到目前为止的东西

因此,使用下面的代码,我制作了一个看起来更杂乱的电子表格,删除重复项,根据公司名称排序,并创建一个包含相关数据列的新 pandas 数据库:

corp_df 是我要使用的清理后的新数据框。

和 recipien_4 是公司的唯一 ID 号,正如您所看到的,它会在每次拨款时重复出现。屏幕截图中的 Folia Biotech 显示了一项重复的资助,正如我未包含在屏幕截图中的一列所证明的那样。有不少重复的,如截图所示。

import pandas as pd

in_file = '2019-20 Grants and Contributions.csv'

# create dataframe 
df = pd.read_csv(in_file)

# sort in order of agreemen_1
df.sort_values("recipien_2", inplace = True)

# remove duplicates
df.drop_duplicates(subset='agreemen_1', keep='first', inplace=True)

corp_dict = { }

# creates empty dict with only 1 copy of all corporation names, all values of 0
for name in corp_df_2['recipien_2']:
    if name not in corp_dict:
        corp_dict[name] = 0

# full name, id, grant $, longitude, latitude
corp_df = df[['recipien_2', 'recipien_4', 'agreemen_2','longitude','latitude']]

任何提示或技巧将不胜感激,.ittertuples() 似乎不是一个好的解决方案,因为我不确定如何过滤和比较数据,或者是否保留数据类型。但随时证明我错了哈哈。

我认为也许有更好的方法来解决这个问题,直接在 Excel 中而不是遍历 pandas 数据帧的行。这是一个非常开放的问题,所以感谢您提供您认为最好的帮助或指导!

使用 group_by 后接 sum 可能最适合您:

corp_df= df.group_by(by=['recipien_2', 'longitude','latitude']).apply(sum, axis=1)

#if you want to transform the index into columns you can add this after as well:
corp_df=corp_df.reset_index()

我看到您正在使用 pandas 读取文件 csv,因此您可以使用以下方法:

Group by

所以你可以创建一个新的数据框,像这样为公司名称分组:

dfnew = dp.groupby(['recipien_2','agreemen_2']).sum()

然后dfnew有值。

文档 Pandas 分组依据: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html