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'
CodePudding user response:
Maybe you can use data.values() to get a list of all the dataframes:
qhn = pd.concat(data.values())
CodePudding user response:
You can concatenate all using dict.values to call all values at once:
out = pd.concat(data.values(), ignore_index=True)
However, it seems data.values() contains lists of DataFrames, in which case, you can flatten the lists using a generator expression then concatenate:
out = pd.concat((df for lst in data.values() for df in lst), ignore_index=True)
