0

I am trying to drop the first column of a transposed DataFrame that i Import from an .xlsx-File, with df.drop(column['a']) but get the error['a'] not found in axis

I am running it in Jupyter Notebooks and Pandas-Version is 0.23.4

import pandas as pd
import numpy as np

df = pd.read_excel('C:/Users/Desktop/Test.xlsx')

df = df.set_index('a').T
df = df.reset_index()

df = df.drop(columns=['a'])

df before transpose:

 ' a 5 4 3 2

 0 b 1 3 4 5

 1 c 7 6 8 7

 2 d 6 4 7 1

df after transpose and reset_index

a i b c d

0 5 1 7 6

1 4 3 6 4

2 3 4 8 7

3 2 5 7 1
To Wo
  • 3
  • 2

1 Answers1

0

When the you have set the index the column 'a' is not in the columns any more. It is an index. See drop parameter https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html

shekoff
  • 46
  • 4
  • okay í guess I understand what i did wrong. And tried to reset the index and changed the Name by df.index.name="Test" and got something like a double line in my df Header. a,b,c,d in the upper line and "Test" below "a". Any Chance to get rid of "a" – To Wo Jan 31 '19 at 13:42
  • A is a name of your index . It can be deleted like this - "del df.index.name" https://stackoverflow.com/questions/29765548/remove-index-name-in-pandas – shekoff Jan 31 '19 at 16:34
  • "del df.index.name" has no effect on "a". df.dtypes returns: a -> ; b->int64; c->int64; d->int64.. By not transposed dataframe the index is not shown by using dtypes.. – To Wo Feb 01 '19 at 07:36
  • I'm not completely sure , but I do guess that if you try to read the file without header - this may work for you . When you read the excel pass header=None parameter . Hope this work for you. – shekoff Feb 01 '19 at 08:33