Curious Student
Curious Student

Reputation: 695

Issue looping through dataframes in Pandas

I have a dict 'd' set up which is a list of dataframes E.g.:

d["DataFrame1"]

Will return that dataframe with all its columns:

    ID    Name
0  123    John
1  548    Eric
2  184    Sam
3  175    Andy

Each dataframe has a column in it called 'Names'. I want to extract this column from each dataframe in the dict and to create a new dataframe consisting of these columns.

df_All_Names = pd.DataFrame()

for df in d:
     df_All_Names[df] = df['Names']

Returns the error:

TypeError: string indices must be integers

Unsure where I'm going wrong here.

Upvotes: 0

Views: 463

Answers (2)

DJK
DJK

Reputation: 9264

Your can use reduce and concatenate all of the columns named ['Name'] in your dictionary of dataframes

Sample Data

from functools import reduce
d = {'df1':pd.DataFrame({'ID':[0,1,2],'Name':['John','Sam','Andy']}),'df2':pd.DataFrame({'ID':[3,4,5],'Name':['Jen','Cara','Jess']})}

You can stack the data side by side using axis=1

reduce(lambda x,y:pd.concat([x.Name,y.Name],axis=1),d.values())

   Name  Name
0  John   Jen
1   Sam  Cara
2  Andy  Jess

Or on top of one an other usingaxis=0

reduce(lambda x,y:pd.concat([x.Name,y.Name],axis=0),d.values())

0    John
1     Sam
2    Andy
0     Jen
1    Cara
2    Jess

Upvotes: 0

BENY
BENY

Reputation: 323226

For example you have df as follow

df=pd.DataFrame({'Name':['X', 'Y']})
df1=pd.DataFrame({'Name':['X1', 'Y1']})

And we create a dict

d=dict()
d['df']=df
d['df1']=df1

Then presetting a empty data frame:

yourdf=pd.DataFrame()

Using items with for loop

for key,val in d.items():
    yourdf[key]=val['Name']

yield :

yourdf
Out[98]: 
  df df1
0  X  X1
1  Y  Y1

Upvotes: 1

Related Questions