Crosstab Not Working with Duplicate Column Labels
See original GitHub issueCode Sample, a copy-pastable example if possible
1. Example with 2 lambdas applied on same column
df = pd.DataFrame([[0, 1], [0, 1], [1, 0]], columns=[‘a’, ‘b’]) df a b 0 0 1 1 0 1 2 1 0
pd.crosstab(df[‘a’].apply(lambda x: x), df[‘a’].apply(lambda x: x+1)) a 1 2 a 1 2 0 2 0 1
2. Example by manually renaming a column
df = pd.DataFrame([[0, 1], [0, 1], [1, 0]], columns=[‘a’, ‘b’]) df a b 0 0 1 1 0 1 2 1 0
pd.crosstab(df[‘a’], df[‘b’]) b 0 1 a 0 0 2 1 1 0
s = df[‘b’] s.name = ‘a’ pd.crosstab(df[‘a’], s) a 0 1 a 0 1 0 1 0 2
In both cases, the output is not the one expected. The crosstab applies one Series to itself.
Problem description
I encountered the problem when using a crosstab on the same column with 2 different lambdas. The crosstab output a crosstab applying the second column to itself. It seems that crosstab is confused by the same name of the 2 series.
The problem is the same when the 2nd column is manually renamed with the name of the 1rst one, before applying the crosstab. See examples.
Expected Output
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None python: 3.6.4.final.0 python-bits: 64 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 69 Stepping 1, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None
pandas: 0.22.0 pytest: 3.3.2 pip: 10.0.0 setuptools: 38.4.0 Cython: 0.27.3 numpy: 1.14.0 scipy: 1.0.0 pyarrow: None xarray: None IPython: 6.2.1 sphinx: 1.6.6 patsy: 0.5.0 dateutil: 2.6.1 pytz: 2017.3 blosc: None bottleneck: 1.2.1 tables: 3.4.2 numexpr: 2.6.4 feather: None matplotlib: 2.1.2 openpyxl: 2.4.10 xlrd: 1.1.0 xlwt: 1.3.0 xlsxwriter: 1.0.2 lxml: 4.1.1 bs4: 4.6.0 html5lib: 1.0.1 sqlalchemy: 1.2.1 pymysql: None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
To be clear, this has nothing to do with
lambda
s, right? In the following,Out[26]
is the expected output, but with the namea
substituted forb
?I made a PR that raises an error when trying to use duplicated column names. Does it make sense to you?