Create multiple pivot tables assigned to dataframes after iterating

Sample data set – org1_df:

    Unnamed: 0  Respondent ID        Start Date          End Date  \
0          218   1.142380e+11    2/6/2023 11:14    2/6/2023 11:19   
1          223   1.142380e+11     2/6/2023 0:21     2/6/2023 0:29   
2          261   1.142350e+11    2/2/2023 13:54    2/2/2023 14:02   
3          303   1.142330e+11   1/30/2023 18:18   1/30/2023 18:31   
4          336   1.142320e+11   1/30/2023 10:30   1/30/2023 10:35   
..         ...            ...               ...               ...   
67        2262   1.141550e+11   10/25/2022 7:38   10/25/2022 8:52   
68        2268   1.141550e+11  10/24/2022 22:33  10/24/2022 22:36   
69        2388   1.141540e+11   10/24/2022 8:49  10/24/2022 10:15   
70        2406   1.141540e+11   10/24/2022 9:28   10/24/2022 9:47   
71        2448   1.141540e+11   10/24/2022 8:41   10/24/2022 8:50   

    Business type?  Business size?  \
0                2               2   
1                2               1   
2                1               1   
3                2               1   
4                3               1   
..             ...             ...   
67               3               1   
68               2               1   
69               2               1   
70               2               1   
71               2               1   

    Which contract type applies to the majority of your organization's sales?  \
0                                                   2                           
1                                                   2                           
2                                                   1                           
3                                                   2                           
4                                                   1                           
..                                                ...                           
67                                                  1                           
68                                                  2                           
69                                                  2                           
70                                                  1                           
71                                                  2                           

    What are your organization's estimated annual sales (US Dollars)?  \
0                                                   2                   
1                                                   3                   
2                                                   3                   
3                                                   2                   
4                                                   5                   
..                                                ...                   
67                                                  3                   
68                                                  4                   
69                                                  1                   
70                                                  3                   
71                                                  1                   

    How long has your organization conducted business with us?  \
0                                                   4            
1                                                   4            
2                                                   4            
3                                                   4            
4                                                   4            
..                                                ...            
67                                                  4            
68                                                  4            
69                                                  4            
70                                                  4            
71                                                  4            

    Survey Question 1  ...  Survey Question 8  Survey Question 9  \
0                 3.0  ...                2.0                3.0   
1                 3.0  ...                3.0                4.0   
2                 5.0  ...                5.0                4.0   
3                 2.0  ...                1.0                2.0   
4                 4.0  ...                5.0                5.0   
..                ...  ...                ...                ...   
67                4.0  ...                3.0                2.0   
68                3.0  ...                4.0                2.0   
69                1.0  ...                2.0                1.0   
70                5.0  ...                4.0                4.0   
71                3.0  ...                2.0                4.0   

    Survey Question 10  Survey Question 11  Survey Question 12  \
0                  3.0                 3.0                 3.0   
1                  2.0                 2.0                 3.0   
2                  3.0                 4.0                 4.0   
3                  2.0                 3.0                 3.0   
4                  4.0                 1.0                 4.0   
..                 ...                 ...                 ...   
67                 2.0                 1.0                 3.0   
68                 1.0                 3.0                 3.0   
69                 1.0                 3.0                 2.0   
70                 4.0                 4.0                 4.0   
71                 5.0                 5.0                 4.0   

    Survey Question 13  Survey Question 14  Survey Question 15  \
0                  3.0                 3.0                 3.0   
1                  3.0                 3.0                 3.0   
2                  4.0                 4.0                 4.0   
3                  2.0                 2.0                 2.0   
4                  5.0                 4.0                 4.0   
..                 ...                 ...                 ...   
67                 4.0                 4.0                 3.0   
68                 3.0                 3.0                 3.0   
69                 1.0                 2.0                 1.0   
70                 4.0                 5.0                 4.0   
71                 4.0                 3.0                 4.0   

    Survey Question 16  Survey Question 17  
0                  3.0                 3.0  
1                  4.0                 3.0  
2                  4.0                 5.0  
3                  2.0                 2.0  
4                  5.0                 4.0  
..                 ...                 ...  
67                 2.0                 2.0  
68                 3.0                 2.0  
69                 3.0                 3.0  
70                 3.0                 4.0  
71                 3.0                 3.0  

[72 rows x 26 columns]
​
cross1 = pd.crosstab(org1_df["Column 1"], org1_df["Column 6"])
cross1

I want to loop through this code replacing “Column 1” and “Column 6” with new columns (“Column 2” and “Column 7”) in order to produce a new crosstab and assign that crosstab to a new dataframe. It works once, manually, very easily. This provides a count of the answers to a Survey Question by category, in this case, Business Type.

1 = Large Business
2 = Small Business
3 = Non-profit 
    cross1 = pd.crosstab(org1_df["Business type?"], org1_df["Survey Question 1"])
print(cross1)
Survey Question 1  1.0  2.0  3.0  4.0  5.0
Business type?                            
1                    0    0    2    7    8
2                    4    2   13   14    5
3                    0    0    3    7    6

I need the iteration so I have multiple dataframes:

cross1
cross2
cross3
cross4
… etc.

demo_questions = 
['End Date',
 'Business type?',
 'Business size?',
 "Which contract type applies to the majority of your organization's sales?",
 "What are your organization's estimated annual sales (US Dollars)?",
 'How long has your organization conducted business with us?']

survey_questions = 
['Survey Question 1',
 'Survey Question 2',
 'Survey Question 3',
 'Survey Question 4',
 'Survey Question 5',
 'Survey Question 6',
 'Survey Question 7',
 'Survey Question 8',
 'Survey Question 9',
 'Survey Question 10',
 'Survey Question 11',
 'Survey Question 12',
 'Survey Question 13',
 'Survey Question 14',
 'Survey Question 15',
 'Survey Question 16',
 'Survey Question 17']

for d, s in [demo_questions, survey_questions]:
    cross[d] = pd.crosstab(org1_df[d], org1_df[s])

I tried the above but received the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[74], line 1
----> 1 for d, s in [demo_questions, survey_questions]:
      2     cross[d] = pd.crosstab(org1_df[d], org1_df[s])

ValueError: too many values to unpack (expected 2)

  • Add a reproducable data frame and you might get an answer

    – 

Leave a Comment