繁体   English   中英

Python Pandas:在字符串消息上输入数值

[英]Python Pandas: Input numeric value on string message

嗨,我想在我的 MESSAGE 变量上输入计算出的数值。 所以基本上我首先计算了我的数值,然后把它放在一个消息字符串上。 下面是我的代码。

#Compute numeric value
LOCATIONS_COUNT = len(loc_df)
#Add the computed numeric value to the message
MESSAGE = "The total locations is LOCATION_COUNT"
print(MESSAGE)
#This was the result
The total locations is LOCATION_COUNT

基本上我需要 output 的结果,即 LOCATION_COUNT 的数值。 例如值为 10。那么 MESSAGE 应该是这样的。

The total locations is 10

有多种方法可以实现这一点。 您可以将 int 转换为字符串:

#Add the computed numeric value to the message
MESSAGE = "The total locations is "+ str(LOCATION_COUNT)
print(MESSAGE)

或者使用某种字符串格式,如下所示:

#Compute numeric value
LOCATION_COUNT = len(loc_df)
#Add the computed numeric value to the message
MESSAGE = f'The total locations is {LOCATION_COUNT}'
print(MESSAGE)

或这个:

#Compute numeric value
LOCATION_COUNT = len(loc_df)
#Add the computed numeric value to the message
MESSAGE = "The total locations is "
print("{}{}".format(MESSAGE, LOCATION_COUNT))

使用格式

LOCATIONS_COUNT = len(loc_df)
MESSAGE = "The total locations is {}".format(LOCATIONS_COUNT)
print(MESSAGE)

或者

MESSAGE = "The total locations is {}".format(len(loc_df))
print(MESSAGE)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM