Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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_Cassandra_Append - Fatal编程技术网

Python 熊猫数据帧追加速度慢,当追加数百个数据帧时,每个数据帧都有数千行

Python 熊猫数据帧追加速度慢,当追加数百个数据帧时,每个数据帧都有数千行,python,pandas,cassandra,append,Python,Pandas,Cassandra,Append,下面代码中的变量“data”包含数百个查询数据库的执行结果。每个执行结果是一天的数据,包含大约7000行数据(列是timestamp和value)。我每天相互追加数据,产生数百万行数据(这数百个追加需要很长时间)。在获得一个传感器的完整数据集后,我将该数据作为列存储在unitdf数据帧中,然后对每个传感器重复上述过程,并将它们全部合并到unitdf数据帧中 我相信追加和合并都是代价高昂的操作。我可能找到的唯一可能的解决方案是将每个列拆分为列表,一旦所有数据添加到列表中,将所有列合并到一个数据框中

下面代码中的变量“data”包含数百个查询数据库的执行结果。每个执行结果是一天的数据,包含大约7000行数据(列是timestamp和value)。我每天相互追加数据,产生数百万行数据(这数百个追加需要很长时间)。在获得一个传感器的完整数据集后,我将该数据作为列存储在unitdf数据帧中,然后对每个传感器重复上述过程,并将它们全部合并到unitdf数据帧中

我相信追加和合并都是代价高昂的操作。我可能找到的唯一可能的解决方案是将每个列拆分为列表,一旦所有数据添加到列表中,将所有列合并到一个数据框中。有什么加快速度的建议吗

i = 0
for sensor_id in sensors: #loop through each of the 20 sensors
    #prepared statement to query Cassandra
    session_data = session.prepare("select  timestamp, value from measurements_by_sensor where unit_id = ? and sensor_id = ? and date = ? ORDER BY timestamp ASC")
    #Executing prepared statement over a range of dates    
    data = execute_concurrent(session, ((session_data, (unit_id, sensor_id, date)) for date in dates), concurrency=150, raise_on_first_error=False)

    sensordf = pd.DataFrame()
    #Loops through the execution results and appends all successful executions that contain data
    for (success, result) in data:
        if success:
          sensordf = sensordf.append(pd.DataFrame(result.current_rows))

    sensordf.rename(columns={'value':sensor_id}, inplace=True) 
    sensordf['timestamp'] = pd.to_datetime(sensordf['timestamp'], format = "%Y-%m-%d %H:%M:%S", errors='coerce')
    if i == 0:
        i+=1
        unitdf = sensordf
    else:
        unitdf = unitdf.merge(sensordf, how='outer')

“熊猫数据帧追加速度慢,当追加数百个数据帧时,每个数据帧都有数千行”……这就是它的用途。将它们收集到列表或字典中,然后
pd.concat(收集的数据帧)
。这个问题有很多重复。谢谢@piRSquared,很抱歉重复了这个问题。我想concat只用于两个数据帧,而不是它们的列表或字典。不要担心重复,它不会伤害我(-:我提到它主要是为了其他可能想查找它的人,我可以锤击它。