Tasos
Tasos

Reputation: 7577

Create a new column of a dataframe based on rules in Python Pandas

I have a CSV file and I am trying to solve my problem on Pandas. I solved it using pure Python, but I cannot do it with Pandas.

My CSV file has 5 columns. I want to create a new by using the data of one of the others. The contents of the column are [0,1,2,3,4,5]. So, based on the value, I want to do the following:

if value == 0:
   cost_new_column = 0
elif value == 1:
   cost_new_column = 1000
elif value == 2:
   cost_new_column = 2500
...

Doing it in pure Python using for and if is simple. How can I do it in Pandas?

Upvotes: 2

Views: 1160

Answers (1)

EdChum
EdChum

Reputation: 394051

I'd construct a dict of how you want to map the values and call map on the column, example:

In [95]:
df = pd.DataFrame({'a':np.random.randint(0, 6, 20)})
df

Out[95]:
    a
0   5
1   3
2   3
3   5
4   5
5   4
6   0
7   5
8   1
9   0
10  5
11  2
12  4
13  5
14  2
15  5
16  0
17  5
18  4
19  4

In [96]:    
d = dict(zip(range(6), [0,1000,2500,4000,5000,8000]))
d

Out[96]:
{0: 0, 1: 1000, 2: 2500, 3: 4000, 4: 5000, 5: 8000}

In [98]:    
df['new_col'] = df['a'].map(d)
df

Out[98]:
    a  new_col
0   5     8000
1   3     4000
2   3     4000
3   5     8000
4   5     8000
5   4     5000
6   0        0
7   5     8000
8   1     1000
9   0        0
10  5     8000
11  2     2500
12  4     5000
13  5     8000
14  2     2500
15  5     8000
16  0        0
17  5     8000
18  4     5000
19  4     5000

Upvotes: 3

Related Questions