I replaced the missing values with NaN using lambda following function:
data = data.applymap(lambda x: np.nan if isinstance(x, basestring) and x.isspace() else x)
,where data is the dataframe I am working on.
Using seaborn afterwards,I tried to plot one of its attributes,alcconsumption using seaborn.distplot as follows:
seaborn.distplot(data['alcconsumption'],hist=True,bins=100)
plt.xlabel('AlcoholConsumption')
plt.ylabel('Frequency(normalized 0->1)')
It's giving me the following error:
AttributeError: max must be larger than min in range parameter.
This is a known issue with matplotlib/pylab histograms!
See e.g. https://github.com/matplotlib/matplotlib/issues/6483
where various workarounds are suggested, two favourites (for example from https://stackoverflow.com/a/19090183/1021819) being:
Alternatively, specify bin edges (in this case by anyway making use of
Anan
...):I would definitely handle missing values before you plot your data. Whether ot not to use
dropna()
would depend entirely on the nature of your dataset. Isalcconsumption
a single series or part of a dataframe? In the latter case, usingdropna()
would remove the corresponding rows in other columns as well. Are the missing values few or many? Are they spread around in your series, or do they tend to occur in groups? Is there perhaps reason to believe that there is a trend in your dataset?If the missing values are few and scattered, you could easiliy use dropna(). In other cases I would choose to fill missing values with the previously observed value (1). Or even fill the missing values with interpolated values (2). But be careful! Replacing a lot of data with filled or interpolated observations could seriously interrupt your dataset and lead to very wrong conlusions.
Here are some examples that use your snippet...
... on a synthetic dataset:
Output:
(1) Using forward fill with pandas.DataFrame.fillna(method = ffill)
ffill
will "fill values forward", meaning it will replace thenan
's with the value of the row above.(2) Using interpolation with pandas.DataFrame.interpolate()
Interpolate values according to different methods. Time interpolation works on daily and higher resolution data to interpolate given length of interval.
As you can see, the different methods render two very different results. I hope this will be useful to you. If not then let me know and I'll have a look at it again.
You can use the following line to select the non-NaN values for a distribution plot using seaborn: