Home > Blockchain >  How to set conditions in a data frame and then average the values and replace them?
How to set conditions in a data frame and then average the values and replace them?

Time:02-16

I have a DataFrame.

   a    b 
0  0.5  1
1  2@3  4 
2  1   4@4 

I want to set a condition for each column to check if "@" is present for each row values. And if it is, then I need to split the values and average them and replace them as new values. The values that do not have "@" do not have to go through any of the operations. My result should be:

   a    b 
0  0.5  1
1  2.5  4 
2  1   4 

2@3---need to be split as 2 and 3 and the average of these values need to be taken.

CodePudding user response:

You could stack str.split explode astype(float) to create a MultiIndex Series of dtype float out of df. Then groupby the index, find mean and unstack to build back the DataFrame:

out = (df.stack().str.split('@').explode().astype(float)
       .groupby(level=[0,1]).mean().unstack())

Output:

     a    b
0  0.5  1.0
1  2.5  4.0
2  1.0  4.0

CodePudding user response:

Although it is not an optimal solution, one way could be as following

import pandas as pd

for col in df.columns:
    for idx, i in enumerate(df[col]):
        if '@' in str(i):
            temp = [int(j) for j in str(i).split('@')]
            avg = sum(temp) / len(temp)
            df.loc[idx, col] = num
print(df)
  • Related