Home > Software design >  Incorrect df. iloc[:, 0]
Incorrect df. iloc[:, 0]

Time:07-12

My df has below columns

ID  Number  Name
11  ccc-456  dfg
45  ggt-56   ggg
33  67889    ttt

When I created a new dataframe (need it for merging with another dataframe)

df2 = df[['ID', 'Number']]

I got an error message stating ID is not in the index. But when I print(df), I do see the ID column.

When I ran the index df3 = df.iloc[:, 0] I see first two columns ID and Number in the results

ID
24        32666
188       33432
401       34341
448       34490
510       34713
          ...  
14062    108789
14204    110710
14651    116332
14678    116733
14726    117600

Name: NUMBER, Length: 28149, dtype: int64

Why can i access the ID column?

CodePudding user response:

Your issue:

Based on the information provided, your "ID" column is set to your dataframe index.

If you run this test code, you will get the same error that you described.

test_dict = {
    'ID': [11,45,33],
    'Number': ['ccc-456','ggt-56','67889'],
    'Name': ['dfg','ggg','ttt']
    }

df = pd.DataFrame(test_dict).set_index('ID')
df2 = df[['ID', 'Number']]

Solution

The easiest solution would be to do df.reset_index(inplace = True) before you create df2. This will give you the "ID" as a column so you can reference it as desired with df2 = df[['ID', 'Number']]

  • Related