如何在这个语句中获取变量?

2024-04-24 10:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试向while循环中的pandas序列添加新值。你知道吗

但是字符串格式有语法错误。。。你知道吗

 import pandas as pd

 sClose = pd.Series()

 close = history['candles'][0]['closeMid']
 time = history['candles'][0]['time']

 sClose = s.Close.append(pd.Series(%s,index=[%s]))% (close, time)

如何在每个循环期间动态地将新值放入附加序列中?你知道吗


Tags: 字符串importpandasclosetime格式序列history
2条回答

应该在%s附近使用引号。你知道吗

像这样的东西可以达到目的:

close_str = '%s' % (close, )
time_str = '%s' % (time, )
sClose = sClose.append(pd.Series(close_str,index=[time_str]))

但不确定为什么需要将类型转换为字符串。如果closetime是数字(或datetime),您只需执行以下操作:

sClose = sClose.append(pd.Series(close,index=[time]))

由于%s只在带引号的字符串('string'格式)中使用,因此可以直接在final语句中使用变量名,而不是在final语句中放置一个元变量。你知道吗

sClose = s.Close.append(pd.Series(close,index=[time]))

相关问题 更多 >