英文:
Add string Column B where string exists in column Columns A + np.where() + pandas
问题
我需要在包含多个个体的数据集中添加一个次要ID,以确保条目唯一。为此,我尝试使用`np.where()`,但在实施后我意识到每次都在覆盖上一个条目。这是原始方法的示例:
```python
df = pd.DataFrame({'Example':['1','2','3','4']})
df['Add'] = ''
df['Add'] = np.where(df['Example']== '1', 'One','')
df['Add'] = np.where(df['Example']== '2', 'Two','')
df['Add'] = np.where(df['Example']== '3', 'Three','')
df['Add'] = np.where(df['Example']== '4', 'Four','')
df.head()
作为解决办法,我尝试添加了str.contains('')
,认为当字符串为空时会评估为True
,并且只在这种情况下插入新字符串。如下:
df = pd.DataFrame({'Example':['1','2','3','4']})
df['Add'] = ''
df['Add'] = np.where(df['Example'].str.contains('')== '1', 'One','')
df['Add'] = np.where(df['Example'].str.contains('')== '2', 'Two','')
df['Add'] = np.where(df['Example'].str.contains('')== '3', 'Three','')
df['Add'] = np.where(df['Example'].str.contains('')== '4', 'Four','')
df.head()
在这种情况下,所有内容都被填充为空字符串...
有没有一种简单的方法在使用np.where()
写入之前检查单元格是否为空?
<details>
<summary>英文:</summary>
I need to add a secondary ID in a dataset with unique entries for multiple individuals. To do so I am trying to use `np.where()`, after I implemented I realized I am overwriting the last entry each time. This an example of the original approach:
df = pd.DataFrame({'Example':['1','2','3','4']})
df['Add'] = ''
df['Add'] = np.where(df['Example']== '1', 'One','')
df['Add'] = np.where(df['Example']== '2', 'Two','')
df['Add'] = np.where(df['Example']== '3', 'Three','')
df['Add'] = np.where(df['Example']== '4', 'Four','')
df.head()
As a work around I tried adding `str.contains('')` thinking that would evaluate `True` when string is empty and only insert new string in that case. As below:
df = pd.DataFrame({'Example':['1','2','3','4']})
df['Add'] = ''
df['Add'] = np.where(df['Example'].str.contains('')== '1', 'One','')
df['Add'] = np.where(df['Example'].str.contains('')== '2', 'Two','')
df['Add'] = np.where(df['Example'].str.contains('')== '3', 'Three','')
df['Add'] = np.where(df['Example'].str.contains('')== '4', 'Four','')
df.head()
In that instance everything is being filled with an empty string...
Is there a simple method to check if a cell is empty before writing with `np.where()`?
</details>
# 答案1
**得分**: 2
使用 [`map`][1] 函数:
```python
dmap = {'1': 'One', '2': 'Two', '3': 'Three', '4': 'Four'}
df['Add'] = df['Example'].map(dmap).fillna('')
输出:
>>> df
Example Add
0 1 One
1 2 Two
2 3 Three
3 4 Four
英文:
Use map
:
dmap = {'1': 'One', '2': 'Two', '3': 'Three', '4': 'Four'}
df['Add'] = df['Example'].map(dmap).fillna('')
Output:
>>> df
Example Add
0 1 One
1 2 Two
2 3 Three
3 4 Four
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论