I have a DataFrame with about 25 columns, several of which hold data unsuitable for plotting. DataFrame.hist() throws errors on those. How can I specify that those columns should be excluded from the plotting?
Using DataFrame.Select the columns from the original DataFrame and copy it to create a new DataFrame using copy() function. Yields below output. Alternatively, You can also use DataFrame. filter() method to create a copy and create a new DataFrame by selecting specific columns.
Select All Except One Column Using drop() Method in pandas Note that drop() is also used to drop rows from pandas DataFrame. In order to remove columns use axis=1 or columns param. For example df. drop("Discount",axis=1) removes Discount column by kepping all other columns untouched.
Use drop() method to delete rows based on column value in pandas DataFrame, as part of the data cleansing, you would be required to drop rows from the DataFrame when a column value matches with a static value or on another column value.
Note, a modification to @Chang She's response, as of pandas 0.16, the -
operator is scheduled for deprecation. The difference()
method is encouraged in its place.
exclude = ['bad col1', 'bad col2'] df.loc[:, df.columns.difference(exclude)].hist()
Update on deprecation:
df - df['A']
is now deprecated and will be removed in a future release. The preferred way to replicate this behavior is
df.sub(df['A'], axis=0)
I imagine you could just:
df.drop(['bad col1', 'bad col2', 'bad col3', ...], axis=1).hist()
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