【问题标题】:Select rows in dataframes inside a list then append to another dataframe inside another list选择列表内数据框中的行,然后附加到另一个列表中的另一个数据框
【发布时间】:2020-10-27 03:02:44
【问题描述】:

我有一个包含 n 个数据框的列表中的每日股票数据(每只股票都有自己的数据框)。我想从每个数据帧中以相等的时间间隔选择 m 行并将它们附加到另一个列表中的数据帧。基本上,新列表应该有 m 个数据框 - 这是天数,每个数据框长度 n - 股票数量。 我尝试使用嵌套的 for 循环,但它不起作用

cross_section = []
cross_sections_list = []

for m in range(0, len(datalist[0]), 100):    
    for n in range(len(datalist)):
        cross_section.append(datalist[n].iloc[m])
        cross_sections_list.append(cross_section)

这段代码没有做任何事情。我的机器只是堆在上面。如果有其他方法,例如多索引,我也很想尝试。

例如

输入:

[
             Adj Close   Ticker  
 Date                           
 2020-06-01  321.850006   AAPL  
 2020-06-02  323.339996   AAPL  
 2020-06-03  325.119995   AAPL  
 2020-06-04  322.320007   AAPL  
 2020-06-05  331.500000   AAPL  
 2020-06-08  333.459991   AAPL  
 2020-06-09  343.989990   AAPL  
 2020-06-10  352.839996   AAPL  ,

             Adj Close    Ticker  
 Date                           
 2020-06-01  182.830002   MSFT  
 2020-06-02  184.910004   MSFT  
 2020-06-03  185.360001   MSFT  
 2020-06-04  182.919998   MSFT  
 2020-06-05  187.199997   MSFT  
 2020-06-08  188.360001   MSFT  
 2020-06-09  189.800003   MSFT  
 2020-06-10  196.839996   MSFT  ]

输出:

 [
             Adj Close   Ticker  
 Date                           
 2020-06-01  321.850006   AAPL  
 2020-06-01  182.830002   MSFT  ,

             Adj Close   Ticker  
 Date                           
 2020-06-03  325.119995   AAPL  
 2020-06-03  185.360001   MSFT  ,

             Adj Close   Ticker  
 Date                           
 2020-06-05  331.500000   AAPL  
 2020-06-05  187.199997   MSFT  ]

等等。

谢谢

【问题讨论】:

  • 您的意思是将步长设置为 100 吗?
  • 请添加数据输入和预期输出的示例。
  • user13802115,是的
  • 埃德加·拉米雷斯,做到了。谢谢

标签: pandas indexing nested-loops data-transform


【解决方案1】:
data_global = pd.DataFrame()
for i in datalist:
    data_global = data_global.append(i) #first merge all dataframes into one

data_by_date = [i for x, i in data_global.groupby('Date')] #step 2: group the data by date

each_nth_day = []
for i in range(0, len(data_by_date), 21):
    each_nth_day.append(data_by_date[i]) #lastly take each n-th dataframe (21 in this case)

感谢您的帮助 user13802115

【讨论】:

    【解决方案2】:

    不完全清楚你想要什么,但这里有一些代码希望能有所帮助。

    list_of_df = [] #list of all the df's
    
    alldf = pd.concat(list_of_df) #brings all df's into one df
    
    list_of_grouped = [y for x, y in alldf.groupby('Date')] #separates df's by date and puts them in a list
    
    number_of_days = alldf.groupby('Date').ngroups #Total number of groups (Dates)
    
    list_of_Dates = [x for x, y in alldf.groupby('Date')] #List of all the dates that were grouped
    
    count_of_stocks = []
    for i in range(len(list_of_grouped)):
        count_of_stocks.append(len(list_of_grouped[i])) #puts count of each grouped df into a list
    
    zipped = list(zip(list_of_data,count_of_stocks)) #combines the dates and count of stocks in a list to see how many stocks are in each date.
    

    【讨论】:

    • 感谢它工作得很好。我简化了一点。我不能做的就是第三步。将发布答案
    • 很高兴我能帮上忙。请参考以下链接,了解如何关闭问题。 stackoverflow.com/help/someone-answers
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2021-12-13
    • 2022-07-31
    • 1970-01-01
    相关资源
    最近更新 更多