-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Replacing multiple columns (or just one) with iloc does not work #22046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I believe the problem is that the column names do not coincide. The following two versions both work on my machine: import pandas
columns = pandas.DataFrame({'b1': [11, 12, 13], 'b2': [14, 15, 16]})
inputs = pandas.DataFrame({'a1': [1, 2, 3], 'b1': [4, 5, 6], 'c1': [7, 8, 9]})
inputs.iloc[:, [1]] = columns.iloc[:, [0]]
print(inputs) import pandas
columns = pandas.DataFrame({'a2': [11, 12, 13], 'b2': [14, 15, 16]})
inputs = pandas.DataFrame({'a1': [1, 2, 3], 'b1': [4, 5, 6], 'c1': [7, 8, 9]})
inputs.iloc[:, [1]] = columns.iloc[:, [0]].values
print(inputs) |
Check out the big red warning in the docs. Basically, pandas is trying to set the |
I can understand that this happens with |
Here's my understanding:
to
|
Sure, but there is difference between I worry that going through |
Hmm, so there is really two independent issues here?
|
Yes, in principle |
Related issues: #12991 |
Seems that Input: columns = pandas.DataFrame({'a2': [11, 12, 13], 'b2': [14, 15, 16]})
inputs = pandas.DataFrame({'a1': [1, 2, 3], 'b1': [4, 5, 6], 'c1': [7, 8, 9]})
inputs.iloc[:, 1] = columns.iloc[:, 0]
print(inputs) Output:
|
Not really. ;-) But that is another bug: #22036 |
Thanks. |
Code Sample, a copy-pastable example if possible
Problem description
I have a code which is replacing a set of columns with another set of columns, based on column indices. To make things done without a special case, I assumes I could just use
iloc
to both select and set columns in a DataFrame. But it seems that this not work and fails in strange ways.Expected Output
But in reality, you get:
See how values converted to float and how column is
NaN
s?But, if I do the following I get expected results:
This also works:
So if it works with lists and ndarrays, one would assume it would also work with DataFrames themselves. But it does not.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.13.0-46-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.23.3
pytest: None
pip: 18.0
setuptools: 40.0.0
Cython: None
numpy: 1.15.0
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: