Python 如何在这个pandas语句中获取变量?
我试图在while循环中向pandas系列添加新值 但是我在字符串格式上有个语法错误Python 如何在这个pandas语句中获取变量?,python,pandas,string-formatting,Python,Pandas,String Formatting,我试图在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) 如何在每次循环过程中动态地
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)
如何在每次循环过程中动态地将新值放入附加序列中?由于
%s
仅用于带引号的字符串(“字符串”格式),因此您可以直接在final语句中使用变量名,而不是放置元变量来保留位置
sClose = s.Close.append(pd.Series(close,index=[time]))
您应该在
%s
周围使用引号
像这样的东西可以达到目的:
close_str = '%s' % (close, )
time_str = '%s' % (time, )
sClose = sClose.append(pd.Series(close_str,index=[time_str]))
但不确定为什么需要将类型转换为字符串。如果close
和time
是数字(或datetime),您只需执行以下操作:
sClose = sClose.append(pd.Series(close,index=[time]))
您使用的
%s
仅用于引号内的字符串,而不是您显示的打开语句。我从未见过字符串外的字符串格式。您在此处提供的信息不足。环路在哪里?什么是s
?您的%s
语句在字符串之外,是否正在尝试在那里格式化字符串?