如何创建新列并按列组分配值

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

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

如果uidnew之间存在相关性,您可以创建一个函数来定义uidnew之间的映射。

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&#39;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({&#39;uid&#39;: [1, 2, 3]})
df[&quot;new&quot;] = np.where(df[&quot;uid&quot;] % 3 == 0, 1, 0)
print(df)

Output:

   uid  new
0    1    0
1    2    0
2    3    1

huangapple
  • 本文由 发表于 2023年2月6日 21:14:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361783.html
匿名

发表评论

匿名网友

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

确定