英文:
How can I duplicate the same row but with different information in a column in pandas?
问题
这是您的代码部分的翻译:
我的pandas数据框如下所示:
```python
import pandas as pd
table = pd.DataFrame({'Range': ["A", "B", "C", "A"], 'First Name': ["W","X","Y", "Z"], 'ID': [1,2,3,4]})
我想要复制相同的行,如果在"Level"列中有文本"A",则在"Activity"列中添加文本"Monitoring"和"Informant",类似于这样:
我尝试使用以下代码进行复制:
columns_new = pd.DataFrame(columns=["NO ID","Level", "Name", "Activity"])
row_modified = []
for index, row in table.iterrows():
rang = row['Range']
f_name= row['First Name']
n_id = row['ID']
columns_new.loc[index, "NO ID"] = n_id
columns_new.loc[index, "Level"] = rang
columns_new.loc[index, "Name"] = f_name
if rang == "A":
row_modified.append(row)
row_modified.append(row)
else:
row_modified.append(row)
column_new2 = pd.DataFrame(row_modified)
column_new2
但我不知道如何添加我想要的文本 😅
如果您需要进一步的帮助或有其他问题,请随时告诉我。
<details>
<summary>英文:</summary>
My pandas dataframe looks like this:
import pandas as pd
table = pd.DataFrame({'Range': ["A", "B", "C", "A"],'First Name': ["W","X","Y", "Z"], 'ID': [1,2,3,4]})
![Initial state of dataframe](https://i.stack.imgur.com/XxQ5V.png)
I want to replicate the same row if in the "Level" column I have the text "A", but in the "Activity" column add the text "Monitoring" and "Informant", something like this:
![Desired output of data transformation example](https://i.stack.imgur.com/XXUbN.png)
I tried to make the duplicate with this code
columns_new = pd.DataFrame(columns=["NO ID","Level", "Name", "Activity"])
row_modified = []
for index, row in table.iterrows():
rang = row['Range']
f_name= row['First Name']
n_id = row['ID']
columns_new.loc[index, "NO ID"] = n_id
columns_new.loc[index, "Level"] = rang
columns_new.loc[index, "Name"] = f_name
if rang == "A":
row_modified.append(row)
row_modified.append(row)
else:
row_modified.append(row)
column_new2 = pd.DataFrame(row_modified)
column_new2
![Dataframe result of code so far](https://i.stack.imgur.com/TSbpE.png)
But I don't know how to add the texts I want 😅
</details>
# 答案1
**得分**: 2
你可以使用 [`merge`](https://pandas.pydata.org/docs/reference/api/pandas.merge.html) 函数:
```python
s = pd.Series(['Monitoring', 'Informant'], index=['A', 'A'], name='Activity')
(table.merge(s, left_on='Range', right_index=True, how='left')
.fillna({'Activity': 'Assistant'})
)
输出结果:
Range First Name ID Activity
0 A W 1 Monitoring
0 A W 1 Informant
1 B X 2 Assistant
2 C Y 3 Assistant
3 A Z 4 Monitoring
3 A Z 4 Informant
英文:
You can use a merge
:
s = pd.Series(['Monitoring', 'Informant'], index=['A', 'A'], name='Activity')
(table.merge(s, left_on='Range', right_index=True, how='left')
.fillna({'Activity': 'Assistant'})
)
Output:
Range First Name ID Activity
0 A W 1 Monitoring
0 A W 1 Informant
1 B X 2 Assistant
2 C Y 3 Assistant
3 A Z 4 Monitoring
3 A Z 4 Informant
答案2
得分: 1
你可以使用一个映射字典:
d = {'A': ['监控', '通知者']}
out = (table.assign(活动=table['范围'].map(d).fillna('助手'))
.explode('活动'))
print(out)
# 输出结果
范围 姓 ID 活动
0 A W 1 监控
0 A W 1 通知者
1 B X 2 助手
2 C Y 3 助手
3 A Z 4 监控
3 A Z 4 通知者
英文:
You can use a mapping dict:
d = {'A': ['Monitoring', 'Informant']}
out = (table.assign(Activity=table['Range'].map(d).fillna('Assistant'))
.explode('Activity'))
print(out)
# Output
Range First Name ID Activity
0 A W 1 Monitoring
0 A W 1 Informant
1 B X 2 Assistant
2 C Y 3 Assistant
3 A Z 4 Monitoring
3 A Z 4 Informant
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论