pandas histogram plot error: ValueError: num must be 1 <= num <= 0, not 1

前端 未结 1 1287
梦如初夏
梦如初夏 2021-01-07 06:59

I am drawing a histogram of a column from pandas data frame:

%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
df.hist(column=\'column_A         


        
相关标签:
1条回答
  • 2021-01-07 07:45

    Problem

    The problem you encounter arises when column_A does not contain numeric data. As you can see in the excerpt from pandas.plotting._core below, the numeric data is essential to make the function hist_frame (which you call by DataFrame.hist()) work correctly.

    def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
                   xrot=None, ylabelsize=None, yrot=None, ax=None, sharex=False,
                   sharey=False, figsize=None, layout=None, bins=10, **kwds):
        # skipping part of the code
        # ...
        if column is not None:
            if not isinstance(column, (list, np.ndarray, Index)):
                column = [column]
            data = data[column]
        data = data._get_numeric_data()  # there is no numeric data in the column
        naxes = len(data.columns)  # so the number of axes becomes 0
        # naxes is passed to the subplot generating function as 0 and later determines the number of columns as 0
        fig, axes = _subplots(naxes=naxes, ax=ax, squeeze=False,
                              sharex=sharex, sharey=sharey, figsize=figsize,
                              layout=layout)
        # skipping the rest of the code
        # ...
    

    Solution

    1. If your problem is to represent numeric data (but not of numeric dtype yet) with a histogram, you need to cast your data to numeric, either with pd.to_numeric or df.astype(a_selected_numeric_dtype), e.g. 'float64', and then proceed with your code.

    2. If your problem is to represent non-numeric data in one column with a histogram, you can call the function hist_series with the following line: df['column_A'].hist(bins=100).

    3. If your problem is to represent non-numeric data in many columns with a histogram, you may resort to a handful options:

      • Use matplotlib and create subplots and histograms directly
      • Update pandas at least to version 0.25
    0 讨论(0)
提交回复
热议问题