给出一个包含Product Id
和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
我要添加一个基于Amount
的“说明”列,该列应如下所示:
Product Id Amount Description
0 504145 12000.0 Amount is 12000.0
1 555933 23010.5 Amount is 23010.5
当我使用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...
但是,它可以与使用+
的简单字符串连接一起很好地工作:
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
为什么在Pandas DataFrame中格式化f字符串会表现出这样的行为?我应该如何修复它以使用f字符串格式?还是不建议在熊猫中使用f字符串格式进行字符串连接?
答案 0 :(得分:2)
您需要按每个值进行迭代,例如由apply
:
df['Description'] = df["Amount"].apply(lambda x: f'Amount is {x}')
或通过列表理解:
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
您的解决方案:
df['Description'] = f'Amount is {df["Amount"].astype(str)}'
工作方式不同-它将Series的每个值(也带有索引)附加到字符串,并对新列的所有值像常量一样重复。