【问题标题】:How to concatenate data frames from two different dictionaries into a new data frame in python?python - 如何将来自两个不同字典的数据帧连接成python中的新数据帧?
【发布时间】:2020-03-02 16:50:29
【问题描述】:

这是我的示例代码

dataset_current=dataset_seq['Motor_Current_Average']
dataset_consistency=dataset_seq['Consistency_Average']

#technique with non-overlapping the values(for current)
dataset_slide=dataset_current.tolist()
from window_slider import Slider
import numpy
list = numpy.array(dataset_slide)
bucket_size = 336
overlap_count = 0
slider = Slider(bucket_size,overlap_count)
slider.fit(list)      
empty_dictionary = {}
count = 0
while True:
  count += 1
  window_data = slider.slide()
  empty_dictionary['df_current%s'%count] = window_data
  empty_dictionary['df_current%s'%count] =pd.DataFrame(empty_dictionary['df_current%s'%count])
  empty_dictionary['df_current%s'%count]= empty_dictionary['df_current%s'%count].rename(columns={0: 'Motor_Current_Average'})
  if slider.reached_end_of_list(): break
  locals().update(empty_dictionary)


#technique with non-overlapping the values(for consistency)
dataset_slide_consistency=dataset_consistency.tolist()
list = numpy.array(dataset_slide_consistency)
slider_consistency = Slider(bucket_size,overlap_count)
slider_consistency.fit(list)      
empty_dictionary_consistency = {}
count_consistency = 0
while True:
  count_consistency += 1
  window_data_consistency = slider_consistency.slide()
  empty_dictionary_consistency['df_consistency%s'%count_consistency] = window_data_consistency
  empty_dictionary_consistency['df_consistency%s'%count_consistency] =pd.DataFrame(empty_dictionary_consistency['df_consistency%s'%count_consistency])
  empty_dictionary_consistency['df_consistency%s'%count_consistency]= empty_dictionary_consistency['df_consistency%s'%count_consistency].rename(columns={0: 'Consistency_Average'})
  if slider_consistency.reached_end_of_list(): break
  locals().update(empty_dictionary_consistency)
import pandas as pd
output_current ={}
increment = 0
while True:
   increment +=1
   output_current['dataframe%s'%increment] = pd.concat([empty_dictionary_consistency['df_consistency%s'%count_consistency],empty_dictionary['df_current%s'%count]],axis=1)

我的问题是我有两个字典,每个字典包含 79 个数据帧,即 "empty_dictionary_consistency" 和 "empty_dictionary" 。我想为它们中的每一个创建一个新的数据框,以便将 empty_dictionary_consistency 的 df1 与 empty_dictionary 的 df1 连接起来。所以,它将从连接 的 df1 开始>empty_dictionary_consistency 和 df1 从 empty_dictionary 到 df79 从 empty_dictionary_consistency 和 df79 从 empty_dictionary 。我尝试使用 while 循环来增加它,但没有显示任何输出。

output_current ={}
increment = 0
while True:
   increment +=1
   output_current['dataframe%s'%increment] = pd.concat([empty_dictionary_consistency['df_consistency%s'%count_consistency],empty_dictionary['df_current%s'%count]],axis=1) 

有人可以帮我解决这个问题吗?我该怎么做。

【问题讨论】:

    标签: python-3.x pandas numpy dataframe while-loop


    【解决方案1】:

    我现在不在我的电脑附近,所以我无法测试代码,但似乎问题出在索引上。在最后一个循环中,每次迭代都会增加一个名为“increment”的变量,但您仍然使用先前循环中的索引来连接要连接的字典。尝试将用于索引所有字典的变量更改为“增量”。 还有一件事 - 我看不到这个循环什么时候结束?

    UPD 我的意思是:

    length = len(empty_dictionary_consistency)
    increment = 0 
    while increment < length: 
        increment +=1
        output_current['dataframe%s'%increment] = pd.concat([empty_dictionary_consistency['df_consistency%s'%increment],empty_dictionary['df_current%s'%increment]],axis=1) 
    

    在遍历你的字典时,你应该使用一个你递增的变量作为所有三个字典中的索引。并且一旦你不在循环中使用 Slider 对象,你必须在第一个字典结束时停止它。

    【讨论】:

    • 能否请您尝试执行代码,它将真正帮助您理解。您也可以编写代码以便我更好地理解。这有点令人困惑..或者有没有其他方法可以做我想做的事情。
    • @Logica 我现在无法正确运行代码,但我编辑了我的答案 - 在代码中写下了我的意思。
    • @Logica 请告诉我代码是否解决了您的问题。
    • 一个问题你在哪里使用 i 变量? while i
    • 接受你的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2020-07-26
    • 2021-10-15
    • 2018-12-30
    相关资源
    最近更新 更多