I know how to drop columns from a data frame using Python. But for my problem the data set is vast, the columns I want to drop are grouped together or are basically singularly spread out across the column heading axis. Is there a shorter way to slice or drop all the columns with fewer lines of code rather than to write it out like how I have done. The way I have done it here works but I would like a more summarized way.
The flight_data_copy_final is the variable in which it should be stored.
Here's my code:
from IPython.display import display
flight_data_copy_version1 = flight_data_copy.drop(flight_data_copy.ix[:,"Year": "FlightDate"].columns, axis=1)
flight_data_copy_version2 = flight_data_copy_version1.drop("TailNum", axis=1)
flight_data_copy_version3 = flight_data_copy_version2.drop("OriginStateFips", axis=1)
flight_data_copy_version4 = flight_data_copy_version3.drop("DestStateFips", axis=1)
flight_data_copy_version5 = flight_data_copy_version4.drop("Diverted", axis=1)
flight_data_copy_version6 = flight_data_copy_version5.drop("Flights", axis=1)
flight_data_copy_final = flight_data_copy.drop(flight_data_copy_version6.ix[:,"FirstDepTime":].columns, axis=1)
print (display (flight_data_copy_final))
We can use Pandas drop() function to drop multiple columns from a dataframe. Pandas drop() is versatile and it can be used to drop rows of a dataframe as well. To use Pandas drop() function to drop columns, we provide the multiple columns that need to be dropped as a list.
pandas drop() method remove the column by name and index from the DataFrame, by default it doesn't remove on the existing DataFrame instead it returns a new DataFrame without the columns specified with the drop method. In order to remove columns on the existing DataFrame object use inplace=True param.
You can delete one or multiple columns of a DataFrame. To delete or remove only one column from Pandas DataFrame, you can use either del keyword, pop() function or drop() function on the dataframe. To delete multiple columns from Pandas Dataframe, use drop() function on the dataframe.
To drop a single column or multiple columns from pandas dataframe in Python, you can use `df. drop` and other different methods. During many instances, some columns are not relevant to your analysis.
To delete multiple columns at the same time in pandas, you could specify the column names as shown below. The option inplace=True
is needed if one wants the change affected column in the same dataframe. Otherwise remove it.
flight_data_copy.drop(['TailNum', 'OriginStateFips',
'DestStateFips', 'Diverted'], axis=1, inplace=True)
Source: Python Pandas - Deleting multiple series from a data frame in one command
df.drop(columns=['col_1', 'col_2','col_N'])
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