Skip to content

Value assignment by iloc overwrites multiple concat columns #12528

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

Closed
JohnNapier opened this issue Mar 4, 2016 · 1 comment
Closed

Value assignment by iloc overwrites multiple concat columns #12528

JohnNapier opened this issue Mar 4, 2016 · 1 comment
Labels
Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@JohnNapier
Copy link

Suppose we wish to concatenate two dataframes with the same name. Because the columns share the same name, loc will not distinguish them. So we use iloc to manipulate them individually.

import pandas as pd,numpy as np
df0=pd.DataFrame(np.arange(10))
df1=pd.DataFrame(np.arange(10,20))
#1) Wrong
df2=pd.concat([df0,df1],axis=1)
df2.iloc[:,0]/=2.

This gives the surprising output that the result on the first column is applied to both.

     0    0
0  0.0  0.0
1  0.5  0.5
2  1.0  1.0
3  1.5  1.5
4  2.0  2.0
5  2.5  2.5
6  3.0  3.0
7  3.5  3.5
8  4.0  4.0
9  4.5  4.5

The expected output is this.

     0   0
0  0.0  10
1  0.5  11
2  1.0  12
3  1.5  13
4  2.0  14
5  2.5  15
6  3.0  16
7  3.5  17
8  4.0  18
9  4.5  19

This could be achieved with this code (although that requires renaming the columns).

df2=pd.concat([df0,df1],axis=1)
df2.columns=range(df2.shape[1])
df2.iloc[:,0]/=2.

Why do we get a different result when we use iloc? I understand the renaming the column would matter if we were using loc. Please help me understand why iloc cares about column names.

I'm using pandas: 0.17.1, numpy: 1.9.2.

Thank you.

@TomAugspurger TomAugspurger added Indexing Related to indexing on series/frames, not to indexes themselves Duplicate Report Duplicate issue or pull request labels Mar 4, 2016
@TomAugspurger
Copy link
Contributor

Pretty sure this was reported in #12344, and is being fixed in #12498. Want to double check that they are indeed the same issue?

@jorisvandenbossche jorisvandenbossche added this to the No action milestone Mar 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

3 participants