-2

I have a DF containing a column with numbers separated by commas (it's a string field).

I need to calculate the mean by row of every value contained in that column.

Example:

"COLUMN 1"
2,18,10
10
80,20

Expected result:

"MEAN"
10
10
50
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
tgrondona
  • 3
  • 2

1 Answers1

0

You can split the comma separated string into list then convert list of string to multiple columns and last calculate the mean along columns.

df['Mean'] = (pd.DataFrame(df['COLUMN 1'].str.split(',').tolist())
              .astype('float').mean(axis=1))
# or
df['Mean'] = (df['COLUMN 1'].str.split(',')
              .apply(pd.Series).astype('float').mean(axis=1))
  COLUMN 1  Mean
0  2,18,10  10.0
1       10  10.0
2    80,20  50.0
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52