Select 列表中数据框中的行然后附加到另一个列表中的另一个数据框中
Select rows in dataframes inside a list then append to another dataframe inside another list
我在 n 个数据框列表中有每日股票数据(每个股票都有自己的数据框)。我想从每个数据帧以相等的时间间隔 select 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 ]
等等。
谢谢
不太清楚你想要什么,但这里有一些代码希望能有所帮助。
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.
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)
感谢用户 13802115 的帮助
我在 n 个数据框列表中有每日股票数据(每个股票都有自己的数据框)。我想从每个数据帧以相等的时间间隔 select 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 ]
等等。
谢谢
不太清楚你想要什么,但这里有一些代码希望能有所帮助。
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.
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)
感谢用户 13802115 的帮助