The dataframe looks like:
ward_wise_gender_df
ward male female other
0 10 10.0 1.0 10.0
1 11 11.0 11.0 10.0
2 7 7.0 7.0 7.0
3 6 6.0 6.0 6.0
4 4 4.0 4.0 4.0
... ... ... ... ...
2532 3 1.0 2.0 2.0
2533 6 1.0 2.0 1.0
2534 7 4.0 2.0 2.0
2535 4 2.0 2.0 2.0
2536 3 3.0 3.0 2.0
2537 rows × 4 columns
My code looks like:
crosstab_table = pd.crosstab(index=ward_wise_gender_df['ward'], columns=['male', 'female', 'other'], values=[ward_wise_gender_df['male'], ward_wise_gender_df['female'], ward_wise_gender_df['other']], aggfunc='sum')
My Issue looks like:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[26], line 1
----> 1 crosstab_table = pd.crosstab(index=ward_wise_gender_df['ward'], columns=['male', 'female', 'other'], values=[ward_wise_gender_df['male'], ward_wise_gender_df['female'], ward_wise_gender_df['other']], aggfunc='sum')
2 crosstab_table
File ~/.local/lib/python3.10/site-packages/pandas/core/reshape/pivot.py:682, in crosstab(index, columns, values, rownames, colnames, aggfunc, margins, margins_name, dropna, normalize)
676 from pandas import DataFrame
678 data = {
679 **dict(zip(unique_rownames, index)),
680 **dict(zip(unique_colnames, columns)),
681 }
--> 682 df = DataFrame(data, index=common_idx)
684 if values is None:
685 df["__dummy__"] = 0
File ~/.local/lib/python3.10/site-packages/pandas/core/frame.py:664, in DataFrame.__init__(self, data, index, columns, dtype, copy)
658 mgr = self._init_mgr(
659 data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy
660 )
662 elif isinstance(data, dict):
663 # GH#38939 de facto copy defaults to False only in non-dict cases
--> 664 mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
665 elif isinstance(data, ma.MaskedArray):
666 import numpy.ma.mrecords as mrecords
File ~/.local/lib/python3.10/site-packages/pandas/core/internals/construction.py:493, in dict_to_mgr(data, index, columns, dtype, typ, copy)
489 else:
490 # dtype check to exclude e.g. range objects, scalars
491 arrays = [x.copy() if hasattr(x, "dtype") else x for x in arrays]
--> 493 return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
File ~/.local/lib/python3.10/site-packages/pandas/core/internals/construction.py:123, in arrays_to_mgr(arrays, columns, index, dtype, verify_integrity, typ, consolidate)
120 index = ensure_index(index)
122 # don't force copy because getting jammed in an ndarray anyway
--> 123 arrays = _homogenize(arrays, index, dtype)
124 # _homogenize ensures
125 # - all(len(x) == len(index) for x in arrays)
126 # - all(x.ndim == 1 for x in arrays)
(...)
129
130 else:
131 index = ensure_index(index)
File ~/.local/lib/python3.10/site-packages/pandas/core/internals/construction.py:620, in _homogenize(data, index, dtype)
615 val = lib.fast_multiget(val, oindex._values, default=np.nan)
617 val = sanitize_array(
618 val, index, dtype=dtype, copy=False, raise_cast_failure=False
619 )
--> 620 com.require_length_match(val, index)
622 homogenized.append(val)
624 return homogenized
File ~/.local/lib/python3.10/site-packages/pandas/core/common.py:571, in require_length_match(data, index)
567 """
568 Check the length of data matches the length of the index.
569 """
570 if len(data) != len(index):
--> 571 raise ValueError(
572 "Length of values "
573 f"({len(data)}) "
574 "does not match length of index "
575 f"({len(index)})"
576 )
ValueError: Length of values (3) does not match length of index (2537)
The Length of columns:
print(len(ward_wise_gender_df['ward']))
print(len(ward_wise_gender_df['male']))
print(len(ward_wise_gender_df['female']))
print(len(ward_wise_gender_df['other']))
Output:
2537
2537
2537
2537