-1

I am doing the data segmentation where I have huge data of 1200 rows and 17 columns. I want to plot the graph for entire data for country and population.

When I am trying to work with below code I am getting an error:

ValueError: could not convert string to float: 'Canada'

The code:

import pandas as pd # for dataframes
import matplotlib.pyplot as plt # for plotting graphs
import seaborn as sns # for plotting graphs
import datetime as dt

data = pd.read_excel("TestData.xls")

plt.figure(1, figsize=(15, 6))
n=0

for x in ['Country', 'Product', 'Sales']:
    n += 1
    plt.subplot(1,3,n)
    plt.subplots_adjust(hspace=0.5, wspace=0.5)
    sns.distplot(data[x], bins=20)
    plt.title('Displot of {}'.format(x))
plt.show()    
Red
  • 26,798
  • 7
  • 36
  • 58
ADS KUL
  • 33
  • 1
  • 9
  • the error is too straightforward you cant plot categorical values using distplot, you can use countplot for categorical features – Yefet May 22 '21 at 12:16
  • Please show the context of `TestData.xls`. – Red May 22 '21 at 12:23
  • Also keep in mind that the [seaborn distplot](https://seaborn.pydata.org/generated/seaborn.distplot.html) will soon be deprecated. You might want to switch to [displot](https://seaborn.pydata.org/generated/seaborn.displot.html#seaborn.displot) or [histplot](https://seaborn.pydata.org/generated/seaborn.histplot.html#seaborn.histplot). – Isotope May 22 '21 at 12:30

1 Answers1

0

If you're passing an object of type str as the first argument in the seaborn.distplot() method, make sure that the string is in the format of an integer or float.

You can try:

import pandas as pd # for dataframes
import matplotlib.pyplot as plt # for plotting graphs
import seaborn as sns # for plotting graphs
import datetime as dt

data = pd.read_excel("TestData.xls")

plt.figure(1, figsize=(15, 6))
n=0

for x in ['Country', 'Product', 'Sales']:
    n += 1
    plt.subplot(1,3,n)
    plt.subplots_adjust(hspace=0.5, wspace=0.5)
    a = data[x]
    if a.replace('.', '', 1).isdigit():
        sns.distplot(a, bins=20)
    else:
        print(f"{a} is not a float.")
    plt.title('Displot of {}'.format(x))
plt.show()    

But do note from the linked documentation:

Warning

This function is deprecated and will be removed in a future version. >Please adapt your code to use one of two new functions:

displot(), a figure-level function with a similar flexibility over the kind of plot to draw

histplot(), an axes-level function for plotting histograms, including with kernel density smoothing

Red
  • 26,798
  • 7
  • 36
  • 58