Reputation: 1088
Suppose I have the following dataframes:
df1 = pd.DataFrame({'col1':['x','y','z','x','x'],'col2':['n1','n2',np.nan,'n3','n2']})
df2 = pd.DataFrame({'col1':['x','y','z','x','x'],'col2':['m1','m2',np.nan,'m3','m2']})
df3 = pd.DataFrame({'col1':['x','y','z','x','x'],'col2':['o1','o2',np.nan,'o3','o2']})
df_list = [df1,df2,df3]
I want to make a crosstab on each element of df_list
as follow:
pd.crosstab(df_list[i]['col1'], df_list[i]['col2'].isna())
If I replace i
by 0,1
or 2
I get the right table. Now I wish to put this in a for loop, namely:
crosstab_list = []
for i in df_list:
crosstab_list.append(pd.crosstab(df_list[i]['col1'], df_list[i]['col2'].isna()))
yet I get the following error,
TypeError: list indices must be integers or slices, not DataFrame
I wonder what am I missing there?
Upvotes: 1
Views: 242
Reputation: 150745
You are thinking to much:
crosstab_list = []
for i in df_list:
crosstab_list.append(pd.crosstab(i['col1'], i['col2'].isna()))
Upvotes: 1