1

I have a pandas dataframe that look like this

It is a large dataset with 1500 rows and 200 columns

I was wondering how can I remove the number before each value in row and column. example The values look like this: 1: 0.345 2: -0.467

I want only the value to be like this: 0.345 -0.467

How can I do that?

Leonor
  • 53
  • 6

1 Answers1

1

Select all columns without first by iloc and for each column apply split and select second value of lists by [1], last cast to float:

df = pd.DataFrame({0: ['4,8,7', '7,6'], 
                   1: ['1: 0.345', '1: 0.345'], 
                   2: ['2: -0.467', '2: -0.467']})
print (df)
       0         1          2
0  4,8,7  1: 0.345  2: -0.467
1    7,6  1: 0.345  2: -0.467

df.iloc[:, 1:] = df.iloc[:, 1:].apply(lambda x: x.str.split(':').str[1]).astype(float)
print (df)
       0      1      2
0  4,8,7  0.345 -0.467
1    7,6  0.345 -0.467

If no NaNs values is possible use cᴏʟᴅsᴘᴇᴇᴅ's solution:

df.iloc[:, 1:] = df.iloc[:, 1:].applymap(lambda x: x.split(':')[-1]).astype(float)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252