简体   繁体   中英

How to append one dataframe to multiple dataframes in list in python?

I have a data frame (df) and a list of data frames(df1,df2..), I want to append df with each df's in list and store in a new list.

I have split a data frame with 1000 rows to multiple small dataframes of 100 rows each

mysplits = [data.loc[i:i+100-1,:] for i in range(0, 1000,100)]
type(mysplits)
#list

I have another data frame with 100 rows

single_set

I want to combine the single_set to each dfs in my splits and store in a new list so that I can access by index, I used below code, but not creating a list

newdfs = []

for i in np.arange(0, 9):
  newdfs [[i]] = mysplits[i].append(pd.DataFrame(data = single_set), ignore_index=True)

It works if I do individually

newdfs = mysplits[1].append(pd.DataFrame(data = single_set), ignore_index=True)

I expect to loop it.

you are doing it incorrectly here is a working code snippet of what you want i suppose

newdfs = [0]*len(mysplits)

for i in np.arange(0, 9):
  newdfs [i] = mysplits[i].append(pd.DataFrame(data = single_set), ignore_index=True)

let me know if thats what you wanted to do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM