根据条件修改列

huangapple go评论69阅读模式
英文:

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[&#39;Census&#39;] = np.where[a[&#39;Population&#39;]&lt;a[&#39;Census&#39;], a[&#39;Population&#39;], 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[&quot;new&quot;] =  np.where(dataframe1[&#39;population&#39;]&lt;dataframe1[&quot;census&quot;], dataframe1[&quot;population&quot;], 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>



huangapple
  • 本文由 发表于 2023年3月12日 16:08:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定