英文:
Modify Column With a Condition
问题
a['Census'] = np.where(a['Population'] < a['Census'], a['Population'], a['Census'])
英文:
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?
答案1
得分: 1
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 ?
英文:
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)
答案2
得分: 0
确实,`np.where`是一个函数,所以你需要使用`np.where(...)`。另外,你还可以使用`DataFrame.where`来替代`Numpy.where`:
```python
a['Census'] = a['Census'].where(a['Population'] < a['Census'], other=1)
print(a)
# 输出
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
输入数据:
a = pd.DataFrame({'Population': Population, 'Census': Census})
print(a)
# 输出
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
<details>
<summary>英文:</summary>
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
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论