kaaldir
kaaldir

Reputation: 61

Change a dataframe column from text to numbers in pandas dataframe

Using pandas, I have a dataframe here which describes some datas about ozone

ozone_data = pd.read_csv('https://www.dropbox.com/s/0s9ui4h90j7xaxg/Dataset_ozone.csv?dl=1', sep=';')

In this dataset, I have a column which name is "pluie". It describes, 'Sec' or 'Pluie'.

I want to change the value of that column from the string 'Sec' to the number 2, and from the string 'Pluie' to the number 1

I don't know what to do, Can someone help me ?

Upvotes: 1

Views: 2735

Answers (2)

ManojK
ManojK

Reputation: 1640

Use pandas.Series.apply with lambda:

ozone_data['pluie'] = ozone_data['pluie'].apply(lambda x: 1 if x == 'Pluie' else 2)

or use numpy.where

df['pluie'] = np.where(df['pluie'] == 'Pluie', 1,2)

There are few more ways to achieve the same result.

Upvotes: 0

whege
whege

Reputation: 1441

Easiest way is to create a dictionary of the mappings with the string value as the key and the number as the value. Then call pd.replace() on the column in question and pass the dictionary as an argument.

EX:

map_dict = {"Sec": 2, "Pluie":1}
ozone_Data['pluie'].replace(map_dict, inplace=True)

Biggest advantage is this allows you to create a mapping for any amount of values.

Upvotes: 2

Related Questions