Python: Float() Argument Must Be A String Or A Number,not 'pandas
Have the following piece of code through which I am trying to plot a graph: import pandas as pd import numpy as np import matplotlib.pyplot as plt import mpld3 my_list = [1,2,3,
Solution 1:
Matplotlib cannot plot category
datatypes. You would need to convert to a string.
plt.bar(df2['Range2'].index.astype(str), df2['Range2'].values)
Solution 2:
The pd.cut
operation yields intervals:
In[11]: pd.cut(df1["Range1"], [0,1,2,3,4,5,6,7,8,9,10,11,df1['Range1'].max()])
Out[11]:
12 (0, 1]
11 (1, 2]
0 (2, 3]
10 (3, 4]
3 (4, 5]
2 (6, 7]
9 (7, 8]
1 (8, 9]
8 (10, 11]
7 (11, 78]
5 (11, 78]
4 (11, 78]
6 (11, 78]
Name: Range1, dtype: category
Categories (12, interval[int64]): [(0, 1] < (1, 2] < (2, 3] < (3, 4] ... (8, 9] < (9, 10] < (10, 11] <
(11, 78]]
When used in the groupby
operation, they are matched based on the index of the cut operation above, and then grouped and summed according to the operation you specified.
As a result, the intervals end up as the index in df2
:
In[14]: df2Out[14]:
Range1Range2Range1
(0, 1] 11
(1, 2] 21
(2, 3] 33
(3, 4] 41
(4, 5] 52
(5, 6] 00
(6, 7] 72
(7, 8] 81
(8, 9] 92
(9, 10] 00
(10, 11] 111
(11, 78] 1694
When you use df2['Range2'].index.values
it will be an array
of these intervals passed as the first argument to bar
, which is not convertible to a float in the way matplotlib expects.
If you are looking to just plot a bar chart of df2.Range2
and you are happy to have the intervals as the axis labels, this will work:
plt.bar(range(len(df2)), df2.Range2.values, tick_label=df2.Range2.index.values)
and produces this image for me:
Post a Comment for "Python: Float() Argument Must Be A String Or A Number,not 'pandas"