Modify Column With a Condition
Question:
I’m trying to modify a column in a dataframe with a condition. I have two columns on it, named ‘Population’ and ‘Census’. I have got millions of data on it and I just need modify those that ‘Population < Census’. Here is the dataframe very reduced.
dataframe1 = pd.DataFrame()
Population = [34,2,1,4542,422,122,55,7876,45,23,1,3]
Census = [12,43,14,6545,63,123,654,33,123,12,99,88]
it’s placed randomly but it serves to explain.
I want to modify the column ‘Census’. Here what I tried.
a['Census'] = np.where[a['Population']<a['Census'], a['Population'], 1]
But I get an TypeError: ‘function’ object is not subscriptable.
How I can modify this in the same column?
Answers:
After np.where you should use ‘(‘ not ‘[‘. This is a function that’s why it is not subscriptable as mentioned in the error. Can you check this one ?
dataframe1["new"] = np.where(dataframe1['population']<dataframe1["census"], dataframe1["population"], 1)
Indeed, np.where
is a function so you have to use np.where(...)
. Alternatively, you can also use DataFrame.where
as replacement of Numpy.where
:
a['Census'] = a['Census'].where(a['Population'] < a['Census'], other=1)
print(a)
# Output
Population Census
0 34 1
1 2 43
2 1 14
3 4542 6545
4 422 1
5 122 123
6 55 654
7 7876 1
8 45 123
9 23 1
10 1 99
11 3 88
Input data:
a = pd.DataFrame({'Population': Population, 'Census': Census})
print(a)
# Output
Population Census
0 34 12
1 2 43
2 1 14
3 4542 6545
4 422 63
5 122 123
6 55 654
7 7876 33
8 45 123
9 23 12
10 1 99
11 3 88
I’m trying to modify a column in a dataframe with a condition. I have two columns on it, named ‘Population’ and ‘Census’. I have got millions of data on it and I just need modify those that ‘Population < Census’. Here is the dataframe very reduced.
dataframe1 = pd.DataFrame()
Population = [34,2,1,4542,422,122,55,7876,45,23,1,3]
Census = [12,43,14,6545,63,123,654,33,123,12,99,88]
it’s placed randomly but it serves to explain.
I want to modify the column ‘Census’. Here what I tried.
a['Census'] = np.where[a['Population']<a['Census'], a['Population'], 1]
But I get an TypeError: ‘function’ object is not subscriptable.
How I can modify this in the same column?
After np.where you should use ‘(‘ not ‘[‘. This is a function that’s why it is not subscriptable as mentioned in the error. Can you check this one ?
dataframe1["new"] = np.where(dataframe1['population']<dataframe1["census"], dataframe1["population"], 1)
Indeed, np.where
is a function so you have to use np.where(...)
. Alternatively, you can also use DataFrame.where
as replacement of Numpy.where
:
a['Census'] = a['Census'].where(a['Population'] < a['Census'], other=1)
print(a)
# Output
Population Census
0 34 1
1 2 43
2 1 14
3 4542 6545
4 422 1
5 122 123
6 55 654
7 7876 1
8 45 123
9 23 1
10 1 99
11 3 88
Input data:
a = pd.DataFrame({'Population': Population, 'Census': Census})
print(a)
# Output
Population Census
0 34 12
1 2 43
2 1 14
3 4542 6545
4 422 63
5 122 123
6 55 654
7 7876 33
8 45 123
9 23 12
10 1 99
11 3 88