0

I am trying to replace the data using Pandas. Following is my aim -

  1. Remove alphabets from temperature and windspeed columns. 32 F will be replaced by 32 and 6 mph will be replaced by 6.
  2. Convert all negative numbers in temperature and windspeed to NaN. -99999 will be converted to NaN.
  3. Convert all 0 in event to No Event

Following is my code

import pandas as pd
import numpy as np
input_data = {'day': ['1/1/2017', '1/4/2017', '1/5/2017', '1/6/2017', 
                         '1/7/2017', '1/8/2017', '1/9/2017', '1/10/2017', '1/11/2017'],
             'temperature': ['32 F', -99999, 28, -99999, '32 C', '38 F', 35, 34, 40],
             'windspeed': ['6 mph', 9, -99999, 7, -88888, -99999, '10 kmph', 8, 12],
             'event': ['Rain', 'Sunny', 'Snow', 0, 'Rain', 'Sunny', 0, 'Cloudy', 'Sunny']
      }
weather2 = pd.DataFrame(input_data)
new_weather = weather2.replace({"temperature":{-99999: np.NaN,
                                          -88888: np.NaN,
                                          '[A-Za-z]':''},
                            "windspeed":{-99999: np.NaN,
                                          -88888: np.NaN,
                                          '[A-Za-z]':''},
                            "event":{0:'No Event'}},regex=True)

The output is as follows -

After code run

Only regex is changed. How can I combine regex with other replace?

2

1 Answer 1

1

Since in the beginning your column type should be string , all the number here should be string type

new_weather = weather2.replace({"temperature":{'-99999': np.NaN,
                                          '-88888': np.NaN,
                                          '[A-Za-z]':''},
                            "windspeed":{'-99999': np.NaN,
                                          '-88888': np.NaN,
                                          '[A-Za-z]':''},
                            "event":{'0':'No Event'}},regex=True) 

If you want to know more information , try

weather2.applymap(type)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much! Making all numbers as string solved the issue.
@Main yep , yw~ :-) happy coding

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.