英文:
Pandas: How can I concatenate 2 columns as a multiline string?
问题
我有一个pandas数据框,想要使用多行字符串来连接它们。以下是我的pandas数据框,
```python
pd.DataFrame([[1,"This is the desc of id 1"],[4,"This is the desc of id 2"]], columns=["id","desc"])
我想要的字符串如下,
functionalRequirement 1{{
id: 1
text: This is the desc of id 1
risk: high
verifymethod: test
}}
我尝试了下面的代码,但它没有工作。
df['res'] = f"""
functionalRequirement {df['id']} {{
id: {df['id']}
text: {df['desc']}
risk: high
verifymethod: test
}}
"""
我该如何做?
<details>
<summary>英文:</summary>
I have a pandas dataframe and I want to concatenate them using multiline string. Below is my pandas dataframe,
```{python}
pd.DataFrame([[1,"This is the desc of id 1"],[4,"This is the desc of id 2"]], columns=["id","desc"])
I wanted string like below,
functionalRequirement 1{{
id: 1
text: This is the desc of id 1
risk: high
verifymethod: test
}}
I tried below code but it didnt work.
df['res'] = f"""
functionaRequirement {df['id']} {{
id: {df['id']}
text: {df['desc']}
risk: high
verifymethod: test
}}
"""
How can I do it?
答案1
得分: 2
Sure, here is the translated code portion:
fstring = """functionaRequirement {id} {{
id: {id}
text: {desc}
risk: high
verifymethod: test
}}"""
df["res"] = df.apply(lambda r: fstring.format(id=r.id, desc=r.desc), axis=1)
>>> print(df.res[0])
functionaRequirement 1 {{
id: 1
text: 这是id 1的描述
risk: high
verifymethod: test
}}
Please note that I have translated the text "This is the desc of id 1" to "这是id 1的描述" in the output.
英文:
Use string formatting and apply it over all rows
fstring = """functionaRequirement {id} {{
id: {id}
text: {desc}
risk: high
verifymethod: test
}}"""
df["res"] = df.apply(lambda r: fstring.format(id=r.id, desc=r.desc), axis=1)
>>> print(df.res[0])
functionaRequirement 1 {
id: 1
text: This is the desc of id 1
risk: high
verifymethod: test
}
答案2
得分: 1
你可以使用列表推导式:
df['res'] = [f"""
functionaRequirement {id} {{
id: {id}
text: {desc}
risk: high
verifymethod: test
}}
""" for id, desc in zip(df['id'], df['desc'])]
或者如果性能很重要:
ID = df['id'].astype(str)
df['res'] = '\nfunctionaRequirement '+ID+' {\n id: '+ID+'\n text: '+df['desc']+'\n risk: high\n verifymethod: test\n}'
输出:
id desc res
0 1 This is the desc of id 1 \nfunctionaRequirement 1 {\n id: 1\n text: This is the desc of id 1\n risk: high\n verifymethod: test\n}
1 4 This is the desc of id 2 \nfunctionaRequirement 4 {\n id: 4\n text: This is the desc of id 2\n risk: high\n verifymethod: test\n}
英文:
You could use a list comprehension:
df['res'] = [f"""
functionaRequirement {id} {{
id: {id}
text: {desc}
risk: high
verifymethod: test
}}
""" for id, desc in zip(df['id'], df['desc'])]
Or if performance matters:
ID = df['id'].astype(str)
df['res'] = '\nfunctionaRequirement '+ID+' {\n id: '+ID+'\n text: '+df['desc']+'\n risk: high\n verifymethod: test\n}'
Output:
id desc res
0 1 This is the desc of id 1 \nfunctionaRequirement 1 {\n id: 1\n text: This is the desc of id 1\n risk: high\n verifymethod: test\n} \n
1 4 This is the desc of id 2 \nfunctionaRequirement 4 {\n id: 4\n text: This is the desc of id 2\n risk: high\n verifymethod: test\n} \n
答案3
得分: 0
你可以使用 string
模板与 to_dict
导出你的数据框作为字典:
import string
TEMPLATE = string.Template("""\
functionaRequirement ${id} {{
id: ${id}
text: ${desc}
risk: high
verifymethod: test
}}""")
df['res'] = list(map(TEMPLATE.substitute, df.to_dict('records')))
# 或者
df['res'] = [TEMPLATE.substitute(rec) for rec in df.to_dict('records')]
输出:
>>> df
id desc res
0 1 This is the desc of id 1 functionaRequirement 1 {{
id: 1
text: This is the desc of id 1
risk: high
verifymethod: test
}}
1 4 This is the desc of id 2 functionaRequirement 4 {{
id: 4
text: This is the desc of id 2
risk: high
verifymethod: test
}}
>>> print(df.loc[0, 'res'])
functionaRequirement 1 {{
id: 1
text: This is the desc of id 1
risk: high
verifymethod: test
}}
英文:
You can use string
template withT to_dict
to export your dataframe as dict:
import string
TEMPLATE = string.Template("""\
functionaRequirement ${id} {{
id: ${id}
text: ${desc}
risk: high
verifymethod: test
}}""")
df['res'] = list(map(TEMPLATE.substitute, df.to_dict('records')))
# OR
df['res'] = [TEMPLATE.substitute(rec) for rec in df.to_dict('records')]
Output:
>>> df
id desc res
0 1 This is the desc of id 1 functionaRequirement 1 {{\n id: 1\n text...
1 4 This is the desc of id 2 functionaRequirement 4 {{\n id: 4\n text...
>>> print(df.loc[0, 'res'])
functionaRequirement 1 {{
id: 1
text: This is the desc of id 1
risk: high
verifymethod: test
}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论