This question would be very basic but I'm struck in dropping a column without a column name. I imported an excel into pandas and the data looked something like below
A B
0 24 -10
1 12 -3
2 17 5
3 63 45
I tried to get rid of the first column (supposed to be index columns) that has no column name, and wish to have the dataframe with just A and B columns, for instance..
When I ran
df.columns
I get the below
Index(['Unnamed: 0', 'A', 'B', dtype='object')
I tried several ways
df = pd.read_excel(r"path", index_col = False)
and
df.reset_index(drop=True, inplace=True)
and
df = df.drop([''], axis=1)
the below line display an error
self.DATA.drop([""], axis=1, inplace=True)
The error for the above line is
name 'self' is not defined
I tried other possible ways. But nothing seems to work. What is the mistake that I'm making? Any help would be highly appreciated.
CodePudding user response:
this should work for the nth
column in your dataframe df.drop(columns=df.columns[n], inplace=True)
, if it's the first columns, so n = 0
.
CodePudding user response:
Try This
empty_index = [" " for i in range(len(d["A"]))]
df = pd.DataFrame(d, index = empty_index)
here the list is empty spaces..
CodePudding user response:
You can try
pd.read_excel('tmp.xlsx', index_col=0)
# Or
pd.read_excel('tmp.xlsx', usecols=lambda x: 'Unnamed' not in x)
# Or
pd.read_excel('tmp.xlsx', usecols=['A', 'B'])