Home > Blockchain >  Split value of a row with equal values based on the unique entry
Split value of a row with equal values based on the unique entry

Time:11-13

I have a dataframe which looks like this:

Name  T1  T2  alpha
A     10  3   30
A     11  5   Nan
A     13  5   Nan
B     5   2   7
B     3   1   Nan

However need to divide the alpha column in equal parts to replace Nan values for each unique names in Name column like this: eg 30 for A becomes 10 each for corresponding row where A is present

Name  T1  T2  alpha
A     10  3   10
A     11  5   10
A     13  5   10
B     5   2   3.5
B     3   1   3.5

I tried using explode but it is not working as I want it to look like, help idea here would help

CodePudding user response:

With groupby and transform

df['alpha'] = pd.to_numeric(df['alpha'],errors = 'coerce').fillna(0).groupby(df['Name']).transform('mean')
df
Out[50]: 
  Name  T1  T2  alpha
0    A  10   3   10.0
1    A  11   5   10.0
2    A  13   5   10.0
3    B   5   2    3.5
4    B   3   1    3.5

CodePudding user response:

This'll get the job done:

df['alpha'] = (df['alpha'] / df['T2']).ffill()

Output:

  Name  T1  T2  alpha
0    A  10   3   10.0
1    A  11   5   10.0
2    A  13   5   10.0
3    B   5   2    3.5
4    B   3   1    3.5
  • Related