简体   繁体   中英

how to concatenate dataframes in a dictionary

I have a dictionary with size of 8, each key (h3-h10) has a dataframe as value.

I want to do pd.concat multiple time through all my dataframes. I did merge 2 dataframes with simple pd.concat, but I don't know how to iterate through all my dataframes

df=pd.concat([data['h3'][0],data['h4'][0]])
print(df)

it gives this as output

my first idea is creating empty dataframe, then using for loop, appending all the rest of dataframes I have h3-h10 as dataframes

qhn = []
i=0
for k in data:
  for i in range (0:len(data)-1):
    qhn = pd.concat([qhn,data[f'h{i}}'][0]])
    i+=1
print(qhn)

EDIT:

I was thinking of creating a series of dataframes with help from for loop, then do pd.concate(series):

for k in data:
  ser.set_value(data[f'{k}'][0])
print(Series)

but it also gave me error :

AttributeError: 'Series' object has no attribute 'set_value'

也许您可以使用data.values()来获取所有数据框的列表:

qhn = pd.concat(data.values())

You can concatenate all using dict.values<\/code> to call all values at once:

out = pd.concat(data.values(), ignore_index=True)

There could be two scenarios to store this base data<\/code> :

while traversing through the values and using .append<\/code> like this :,如下所示:

data1 = {'h1':df1,'h2':df2,'h3':df3}

counter = 0
for value in data1.values():
    if counter == 0:
        output = value
        counter = 1
    else:
        output = output.append(value)

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