crosstab() missing 1 required positional argument: 'columns'
时间: 2023-10-23 22:13:05 浏览: 117
商务应用范例Crosstab共1页.pdf.zip
The error "crosstab() missing 1 required positional argument: 'columns'" occurs when the crosstab() function is called without specifying the "columns" parameter.
The crosstab() function is used to compute a cross-tabulation of two or more factors. It takes two or more arrays, dataframes or Series objects and returns a new dataframe that shows the frequency distribution of the variables.
To fix the error, make sure to specify the "columns" parameter when calling the crosstab() function. For example:
```python
import pandas as pd
# create sample data
data = {'Gender': ['Male', 'Male', 'Female', 'Female', 'Male'],
'Age': ['18-24', '25-34', '35-44', '45-54', '55+'],
'Income': ['<50k', '<50k', '50-100k', '50-100k', '>100k']}
df = pd.DataFrame(data)
# compute crosstabulation
pd.crosstab(df['Gender'], [df['Age'], df['Income']])
```
Output:
```
Age 18-24 25-34 35-44 45-54 55+
Income <50k <50k 50-100k 50-100k >100k
Gender
Female 0 0 1 1 0
Male 1 1 0 0 1
```
阅读全文