matan
matan

Reputation: 461

How to calculate a mean of a list in a column pandas dataframe

i have data look like this

data={"col1":[ [(1,22),(1.5,20),(3,32),(2,21)],
              [(2,24),(2.5,22)],
      [(6,12),(1.3,18),(5,21)],
              [(4,25),(5,33),(7,21),(2,30)]],
"name":["A","B","C","F"]}
df=pd.DataFrame.from_dict(data)
print(df)

I will like to mean the first and the sec numbers in every row (list) two difrent colls so for the first cell i will get new coll that contain (1+1.5+3+2)\4 and one more col that have 22+20+32+21/4

i do somthing like that but its look messy with the loops

for i in df["col1"]:
    mean_list = []
    for first_numb in i:
        mean_list.append(first_numb[0])

any idea?

Upvotes: 4

Views: 1107

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 71689

We can try exploding and creating a new dataframe from the exploded column then calculating mean on level=0

e = df['col1'].explode()
df[['m1', 'm2']] = pd.DataFrame([*e], index=e.index).mean(level=0)

Alternate approach with list comprehension

df[['m1', 'm2']] = pd.DataFrame([[sum(t) / len(t) for t in zip(*l)]
                                 for l in df['col1']], index=df.index)

                                     col1 name     m1     m2
0  [(1, 22), (1.5, 20), (3, 32), (2, 21)]    A  1.875  23.75
1                    [(2, 24), (2.5, 22)]    B  2.250  23.00
2           [(6, 12), (1.3, 18), (5, 21)]    C  4.100  17.00
3    [(4, 25), (5, 33), (7, 21), (2, 30)]    F  4.500  27.25

Performance checks

# Sample df with 40000 rows
df = pd.concat([df] * 10000, ignore_index=True)


%%timeit
e = df['col1'].explode()
pd.DataFrame([*e], index=e.index).mean(level=0)
# 107 ms ± 1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%%timeit
pd.DataFrame([[sum(t) / len(t) for t in zip(*l)] for l in df['col1']], index=df.index)
# 50.5 ms ± 582 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Upvotes: 5

Related Questions