hen*_*gkk 5 python dataframe python-3.x pandas f-string
给定一个带有Product Id和的DataFrame Amount:
df = pd.DataFrame([['504145', 12000.0],
['555933', 23010.5]],
columns=['Product Id', 'Amount'])
df
Out[1]:
Product Id Amount
0 504145 12000.0
1 555933 23010.5
Run Code Online (Sandbox Code Playgroud)
我想基于添加一个“说明”列Amount,该列应如下所示:
Product Id Amount Description
0 504145 12000.0 Amount is 12000.0
1 555933 23010.5 Amount is 23010.5
Run Code Online (Sandbox Code Playgroud)
当我使用f字符串格式设置时,结果是将整个列Amount作为一个序列进行聚合,而不是将特定行的值用于字符串连接:
df['Description'] = f'Amount is {df["Amount"].astype(str)}'
df
Out[2]:
Product Id Amount Description
0 504145 12000.0 Amount is 0 12000.0\n1 23010.5\nName: Am...
1 555933 23010.5 Amount is 0 12000.0\n1 23010.5\nName: Am...
Run Code Online (Sandbox Code Playgroud)
但是,使用+以下命令进行简单的字符串连接也可以正常工作:
df['Description'] = "Amount is " + df["Amount"].astype(str)
df
Out[9]:
Product Id Amount Description
0 504145 12000.0 Amount is 12000.0
1 555933 23010.5 Amount is 23010.5
Run Code Online (Sandbox Code Playgroud)
为什么Pandas DataFrame中的f字符串格式化会表现出这种现象?我应该如何修复它以使用f字符串格式?还是不建议在熊猫中使用f字符串格式进行字符串连接?
您需要按每个值进行迭代,例如apply:
df['Description'] = df["Amount"].apply(lambda x: f'Amount is {x}')
Run Code Online (Sandbox Code Playgroud)
或者通过列表理解:
df['Description'] = [f'Amount is {x}' for x in df["Amount"]]
print (df)
Product Id Amount Description
0 504145 12000.0 Amount is 12000.0
1 555933 23010.5 Amount is 23010.5
Run Code Online (Sandbox Code Playgroud)
您的解决方案:
df['Description'] = f'Amount is {df["Amount"].astype(str)}'
Run Code Online (Sandbox Code Playgroud)
工作方式不同 - 它将系列的每个值(也带有索引)附加到字符串,并像常量一样为新列的所有值重复。
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |