Min
Min

Reputation: 528

adding new column with values calculated from other columns

I was wandering how you could add a new column with values calculated from other columns.

Let's say 'small' is one that has a value less than 5. I want to record this information in a new column called 'small' where this column has value 1 if it is short and 0 otherwise.

I know you can do that with numpy like this :

import pandas as pd
import numpy as np

rawData = pd.read_csv('data.csv', encoding = 'ISO-8859-1')
rawData['small'] = np.where(rawData['value'] < 5, '1', '0')

How would you do the same operation without using numpy? I've read about numpy.where() but I still don't get it.

Help would be really appreciated thanks!

Upvotes: 1

Views: 72

Answers (1)

jezrael
jezrael

Reputation: 862511

You can convert boolean mask to integer and then to string:

rawData['small'] = (rawData['value'] < 5).astype(int).astype(str)

Upvotes: 2

Related Questions