Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用流式数据在数据帧中进行数据拆分_Python_Pandas - Fatal编程技术网

Python 使用流式数据在数据帧中进行数据拆分

Python 使用流式数据在数据帧中进行数据拆分,python,pandas,Python,Pandas,使用以下代码检索股票名称及其ltp df=pd.DataFrame(data=None) def on_ticks(ws, ticks): global df for sc in ticks: token=sc['instrument_token'] name=trd_portfolio[token]['name'] ltp=sc['last_price'] df=df.append([name,ltp],igno

使用以下代码检索股票名称及其ltp

df=pd.DataFrame(data=None)

def on_ticks(ws, ticks):
    global df
    for sc in ticks:
        token=sc['instrument_token']
        name=trd_portfolio[token]['name']
        ltp=sc['last_price']
        df=df.append([name,ltp],ignore_index=True)

print(df)
然而,两个附加项,即名称和ltp都在同一列中提取,因此我无法进一步操作数据。相同的输出如下所示

              0
0     BANKBARODA
1          39.05
2     NATCOPHARM
3         574.55
4     AUROPHARMA
         ...
4249      194.15
4250     FRETAIL
4251        80.9
4252    HDFCLIFE
4253      517.95

[4254 rows x 1 columns]
请建议一种方法,以便我可以将名称和ltp放在两个不同的列中,以便进一步工作。

使用双大括号:

df = df.append([[name, ltp]], ignore_index=True)

谢谢为了更好地展示,可以将列命名为name和ltp,而不是索引号0&1。非常感谢。因为它是for循环中的流式数据,所以我无法停止数据以进行进一步的操作,如排序、买卖等。是否有办法同时完成子量化过程。我已经准备好了异步函数,但不知道如何实现它。如果可能,请提供指导。这将取决于您对数据的具体操作。此外,IIUC不能对流式数据执行聚合操作。正如我提到的,您将无法对流式数据执行groupby、count等操作。也许你可以单独问这个问题,或者用样本输入数据和预期输出来回答这个问题,也许有人能回答。@SherylHohman。同意。将尝试添加解释并更新答案。
# creating a new dataframe with name and ltp values
# however, since the requirement is to have these within individual 
# columns and not rows, using transpose (T) to address it
df1 = pd.DataFrame([name,ltp]).T

# explicitly adding column names to the temp dataframe and the same will repeat
# everytime a new temp dataframe is formed. This will ensure that the new values
# get appended to the correct columns in the aggregated version
df1.columns = ['name', 'ltp']

# append the temp dataframe to the aggregated version to be published as final
df=df.append(df1,ignore_index=True)