Error pandas and matplotlib could not convert string to float

1

I'm trying to draw on a graph the cell phone prices according to their models

For now I have this

import pandas as pd
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

df1 = pd.read_csv('MercadoLibreMejor_items.csv')
print(df1)


x = df1['Modelo']
y = df1['Precio']

#plot
plt.plot(x,y)


plt.show()

The reading of the csv is correct but at the moment of drawing the graph it throws me the following error:

  

ValueError: could not convert string to float: Pixi 3 (4)

I think it's because in the Model column it's a string but I would not know how to format it at float

    
asked by Emanuel Cortez 25.07.2017 в 20:58
source

1 answer

0

I do not know if it is the most descriptive graphic to show this, but the problem is that, as is logical, you need to provide points with your coordinates x e and to get the graph. The solution is to use a range of integers from 0 to the number of elements you have (phone models in this case) and use them to graph.

Subsequently, we change the marks that the x axis must show to show the names of the models. In this case we can use the dataframe index as a value of x and the price for and .

import pandas as pd
import matplotlib.pyplot as plt


df1 = pd.read_csv('MercadoLibreMejor_items.csv', sep = ';')


fig, ax = plt.subplots(1,1) 
x = df1.index
y = df1['Precio']

ax.set_xticks(x)
ax.set_xticklabels(df1['Modelo'], rotation='vertical', fontsize=10)

plt.plot(x,y)
plt.tight_layout()
plt.show()

Exit:

    
answered by 25.07.2017 / 21:52
source