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?

2
  • Oops, closed by accident. Commented Nov 23, 2017 at 6:50
  • 1
    Try this: df.iloc[:, 1:] = df.iloc[:, 1:].applymap(lambda x: x.split(':')[-1]).astype(float) Commented Nov 23, 2017 at 6:51

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks your solution worked and thanks for the link :)
You are welcome! Dont forget upvote solution too. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.