[英]pd.crosstab() inside a for loop
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:我想在df_list
的每个元素上创建一个交叉表,如下所示:
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.如果我将i
替换为0,1
或2
,我会得到正确的表。 Now I wish to put this in a for loop, namely:现在我想把它放在一个for循环中,即:
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?我想知道我在那里错过了什么?
You are thinking to much:你想了很多:
crosstab_list = []
for i in df_list:
crosstab_list.append(pd.crosstab(i['col1'], i['col2'].isna()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.