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

后端 未结 6 2007
后悔当初
后悔当初 2021-02-02 07:52

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 sho

相关标签:
6条回答
  • 2021-02-02 08:14

    I imagine you could just:

    df.drop(['bad col1', 'bad col2', 'bad col3', ...], axis=1).hist()
    
    0 讨论(0)
  • 2021-02-02 08:16

    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)
    
    0 讨论(0)
  • 2021-02-02 08:27

    Following the official docs you can use loc

    df.loc[:,['A','B']]
    

    And you obtain, for instance, only the column 'A' and 'B'. In this way you can select the columns to plot.

    0 讨论(0)
  • 2021-02-02 08:31

    We can potentially make this easier (I created a github issue), but for now you can select out the columns you want to plot:

    df.ix[:, df.columns - to_exclude].hist()
    
    0 讨论(0)
  • 2021-02-02 08:32

    I usually do the following:

    columns = [column for column in df.columns if df[column].dtype == 'float64']
    df = df[columns]
    
    0 讨论(0)
  • 2021-02-02 08:32

    How about just

    df_new = df[df.columns -[cols_to_exclude]]
    df_new.plot()
    
    0 讨论(0)
提交回复
热议问题