英文:
How to create new column and assign values by column group
问题
我有一个数据框,名为
df
uid
1
2
3
...
我想要分配一个新的列,其值为0或1,取决于uid,我将进行分配。
df
uid new
1 0
2 0
3 1
..
英文:
I have dataframe,
df
uid
1
2
3
...
I want to assign a new column, with values 0 or 1 depending on the uid, which I will assign.
df
uid new
1 0
2 0
3 1
..
答案1
得分: 1
不要翻译代码部分。以下是翻译后的内容:
You must explain the underlying logic.
That said, there are many possible ways.
Considering an explicit mapping with map
:
mapper = {1: 0, 2: 0, 3: 1}
df['new'] = df['uid'].map(mapper)
# or
mapper = {0: [1, 2], 1: [3]}
df['new'] = df['uid'].map({k:v for v,l in mapper.items() for k in l})
Or using a list of targets for the 1
with isin
and conversion to int
target = [3]
df['new'] = df['uid'].isin(target).astype(int)
Output:
uid new
0 1 0
1 2 0
2 3 1
英文:
You must explain the underlying logic.
That said, there are many possible ways.
Considering an explicit mapping with map
:
mapper = {1: 0, 2: 0, 3: 1}
df['new'] = df['uid'].map(mapper)
# or
mapper = {0: [1, 2], 1: [3]}
df['new'] = df['uid'].map({k:v for v,l in mapper.items() for k in l})
Or using a list of targets for the 1
with isin
and conversion to int
target = [3]
df['new'] = df['uid'].isin(target).astype(int)
Output:
uid new
0 1 0
1 2 0
2 3 1
答案2
得分: 0
如果uid
和new
之间存在相关性,您可以创建一个函数来定义uid
和new
之间的映射。
def mapping(value):
new_value = value // 2
return new_value
然后可以使用以下方式:
df["new"] = df["uid"].apply(mapping)
或者直接使用:
df["new"] = df["uid"].apply(lambda value: value // 2)
英文:
If there is a correlation between uid
and new
, you can create a function to define the mapping between uid
and new
def mapping(value):
new_value = value // 2
return new_value
Then
df["new"] = df["uid"].apply(mapping)
Or directly
df["new"] = df["uid"].apply(lambda value: value // 2)
</details>
# 答案3
**得分**: 0
从`3个uid的关系`中,我得出一个关系,即可以被3整除的`uid`分配为`1`,否则分配为`0`。(不确定关系是否正确,因为您只提供了3个`uid`的值)
您可以使用`np.where()`函数,格式如下:`np.where(condition, x, y)`,**如果`condition`满足,则分配值`x`,否则分配值`y`**。
```python
import pandas as pd
import numpy as np
df = pd.DataFrame({'uid': [1, 2, 3]})
df["new"] = np.where(df["uid"] % 3 == 0, 1, 0)
print(df)
输出:
uid new
0 1 0
1 2 0
2 3 1
英文:
from the relation of 3 uid's
I came up as a relation the uid
which is divisible by 3 is assign to 1
else 0
. (Not sure the relation is correct or not as you have given only 3 values of uid
)
you can apply np.where()
-> np.where(condition, x, y)
if condition
satisfy it assign value x
else value y
import pandas as pd
import numpy as np
df = pd.DataFrame({'uid': [1, 2, 3]})
df["new"] = np.where(df["uid"] % 3 == 0, 1, 0)
print(df)
Output:
uid new
0 1 0
1 2 0
2 3 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论