Cole
Cole

Reputation: 399

How do you drop a column by index?

When I run this code it drops the first row instead of the first column:

df.drop(axis=1, index=0)

How do you drop a column by index?

Upvotes: 1

Views: 618

Answers (2)

blackraven
blackraven

Reputation: 5627

Using the example

df = pd.DataFrame([
[1023.423,12.59595],
[1000,11.63024902],
[975,9.529815674],
[100,-48.20524597]], columns = ['col1', 'col2'])

    col1        col2
0   1023.423    12.595950
1   1000.000    11.630249
2   975.000     9.529816
3   100.000     -48.205246

If you do df.drop(index=0), the output is dropping row with index 0

    col1    col2
1   1000.0  11.630249
2   975.0   9.529816
3   100.0   -48.205246

If you do df.drop('col1', axis=1), the output is dropping column with name 'col1'

    col2
0   12.595950
1   11.630249
2   9.529816
3   -48.205246

Please remember to use inplace=True where necessary

Upvotes: 1

skbrhmn
skbrhmn

Reputation: 1199

You can use df.columns[i] to denote the column. Example:

df.drop(df.columns[0], axis=1)

Upvotes: 1

Related Questions