Python Forum
Pandas hierarchical sum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas hierarchical sum
#2
This is what I come up with, attempting to answer my one question. I was hoping to avoid loop. There must be a cleaner way.

data = {
    "Cost Elements": [
        "2184AA",
        "2184DA",
        "2184A6",
        "8484XA",
        "8484YA",
        "2184BE",
        "2184B1",
        "8484B2",
        "8484B3",
    ],
    "parent": [None, "2184AA", "2184DA", "2184A6", "2184A6", "2184AA", "2184BE", "2184BE", "2184B1"],
    "spent": [0, 0, 0, 10, 5, 0, 0, 4, 8],
    "total": [0, 0, 0, 0, 0, 0, 0, 0, 0],
}
index = ["1.1", "1.1.1", "1.1.1.0", "1.1.1.0.1", "1.1.1.0.2", "1.2.1", "1.2.1.0", "1.2.1.0.1", "1.2.1.0.2"]

df = pd.DataFrame(data, index=index)
df.reset_index(inplace=True)

grouped = df.groupby(df["parent"])

sequences = df.index
for s in sequences:
    # print(f"Descendants of {df.iat[s, 0]}")
    descendants = df[df["index"].str.startswith(df.iat[s, 0])]
    # print(descendants)
    # print(f"Sum:{descendants['spent'].sum()}")
    df.iat[s, 4] = descendants["spent"].sum()
print("++++++++++++++++")
print(df)
print("++++++++++++++++")
Reply


Messages In This Thread
Pandas hierarchical sum - by mariostg - Jan-26-2024, 03:17 PM
RE: Pandas hierarchical sum - by mariostg - Jan-26-2024, 03:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a hierarchical catalog of Anaconda libraries divided by the type of tasks? AlekseyPython 1 2,255 Nov-25-2020, 09:11 AM
Last Post: Larz60+
  hierarchical MultIndex Python newpyguy 0 15,204 Jan-01-2018, 09:59 PM
Last Post: newpyguy
  Sklearn Agglomerative Hierarchical Clustering - help with array set up pstarrett 4 6,382 Feb-21-2017, 05:05 AM
Last Post: pstarrett

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020