Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude a few columns from a DataFrame plot?

Tags:

python

pandas

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?

like image 874
chernevik Avatar asked Oct 21 '12 23:10

chernevik


People also ask

How do I make a data frame with only certain columns?

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.

How do I drop all columns except one in a DataFrame?

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.

How do you remove certain values from a data frame?

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.


2 Answers

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) 
like image 122
pylang Avatar answered Sep 23 '22 00:09

pylang


I imagine you could just:

df.drop(['bad col1', 'bad col2', 'bad col3', ...], axis=1).hist() 
like image 38
brianray Avatar answered Sep 23 '22 00:09

brianray