2015-06-08 15 views
-1

我试图在while循环中向熊猫系列添加新值。我如何在这个熊猫陈述中得到我的变量?

但我得到的字符串格式化语法错误​​...

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) 

如何动态地把新值附加的系列内的每个循环中?

+0

的'%s'您只使用了带引号的字符串中的,而不是一个公开声明为您展示。 – Cometsong

+0

我从来没有见过字符串格式以外的字符串。 – chrisaycock

+0

您在这里没有提供足够的信息。循环在哪里?什么是's'?你的'%s'语句不在字符串中,你想在那里格式化一个字符串吗? – sedavidw

回答

2

您应该在%s附近使用报价。为什么你需要transtype串

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

,但不知道:

像这样的事情会做的伎俩。如果closetime是数字(或日期时间),你可以简单地做:

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

由于%s仅用于带引号的字符串('字符串'格式),因此您可以直接在最终语句中使用变量名称,而不是将元变量放在那里。

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