I want to select all but the 3 last columns of my dataframe.
I tried :
df.loc[:,-3]
But it does not work
Edit : title
To select all columns except one column in Pandas DataFrame, we can use df. loc[:, df. columns != <column name>].
You can also acheive selecting all columns except one column by deleting the unwanted column using drop() method. Note that drop() is also used to drop rows from pandas DataFrame. In order to remove columns use axis=1 or columns param.
You can also use DataFrame. drop() method to delete the last n columns. Use axis=1 to specify the columns and inplace=True to apply the change on the existing DataFrame.
Select everything EXCEPT the last 3 columns, do this using iloc
:
In [1639]: df
Out[1639]:
a b c d e
0 1 3 2 2 2
1 2 4 1 1 1
In [1640]: df.iloc[:,:-3]
Out[1640]:
a b
0 1 3
1 2 4
Use this df.columns
being sliced, and putted into a df[...]
bracket:
print(df[df.columns[:-3]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With