在Jupyer Lab中使用Pandas,如何将数据帧中的一列值从浮点2379.77修改为货币值$2379.77?

在Jupyer Lab中使用Pandas,如何将数据帧中的一列值从浮点2379.77修改为货币值$2379.77?,pandas,dataframe,Pandas,Dataframe,我正在处理一个赋值,我试图在我的数据框中将该列的一个值显示为currency$。当进行所有计算时,数据显示在浮动上,但我想将其设置为货币值,因为它指的是总收入 如果有人能帮我解决这个问题,我会非常感激。我正在附上我的代码。我附上了返回的汇总表数据的屏幕截图 # I created variables to hold the values to later create the summary table with them. I needed to go back and look at the

我正在处理一个赋值,我试图在我的数据框中将该列的一个值显示为currency$。当进行所有计算时,数据显示在浮动上,但我想将其设置为货币值,因为它指的是总收入

如果有人能帮我解决这个问题,我会非常感激。我正在附上我的代码。我附上了返回的汇总表数据的屏幕截图

# I created variables to hold the values to later create the summary table with them. I needed to go back and look at the decimal places that were used in the solution. To be able to match the solution format I decided to use the method round()
ItemCount = df["Item Name"].nunique()
AveragePrice = round(df["Price"].mean(),2)
PurchasedNumber = df["Purchase ID"].count()
Revenue = round(df["Price"].sum(),2)

#After I created the variables I need to store them in a summary table like so:
SummaryTable = pd.DataFrame([{"Number of Unique Items": ItemCount, "Average Price": AveragePrice, "Number of Purchases": PurchasedNumber, "Total Revenue": Revenue}])
SummaryTable
试试这个:

SummaryTable.loc[:, "Total Revenue"] = SummaryTable["Total Revenue"].map(lambda x: '$' + str(x))

感谢您的及时回复!我试着将它放在print语句之前的SummaryTable下,它运行了,但是有相同的输出(附加代码)。可能是我没有正确输入代码,你能帮我看看吗?先谢谢你。SummaryTable=pd.DataFrame([{“唯一项目数”:ItemCount,“平均价格”:AveragePrice,“购买数量”:PurchasedNumber,“总收入”:Revenue}])SummaryTable[“总收入”].map(lambda x:'$'+str(x))SummaryTable我编辑了我的答案。让我知道它是如何工作的。它正在显示一个与序列不兼容的索引器。我将索引更改为[4],但它仍然显示相同的错误。再次感谢您在这方面的帮助,非常欢迎。现在呢?我更新了我的答案。我很高兴能帮上忙<代码>映射方法作用于序列。作为参数,它可以对
系列中的每个值应用函数。例如,在您的数据帧中,在
SummaryTable[“Total Revenue”]
系列中选择值,并对每个值应用
lambda
函数。实际上,lambda函数中的
x
将是序列中的每个值。例如,序列中的第一个值是
2379.77
,它是一个浮点数。Lambda函数首先使用
str
函数将其更改为字符串,然后将
$
连接到它的开头。我希望现在清楚了。