0
$\begingroup$

I am concerned with a single column (fruit) from my df:

| fruit               |
| --------------------|  
| apple, orange       | 
| banana              |
| grapefruit, orange  |
| apple, banana, kiwi |

I want to plot the values from fruit to a pie chart to get a visual representation of the distribution of each individual fruit

I run: df.plot(kind='pie', y='fruit')

But this gives a TypeError: '<' not supported between instances of 'str' and 'int'

I have read: https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers

But I can't see how it helps solve my problem

Any help much appreciated!

$\endgroup$

2 Answers 2

1
$\begingroup$

You may want first to try to count the number of occurrences of each string inside the column, and from then you have only to plot with whatever kind of plot you want.

df = pd.DataFrame({"fruit":["apple, orange", "banana", "grapefruit, orange", "apple, banana, kiwi"]})

df.fruit.str.get_dummies(sep = ",").sum().plot.pie();
$\endgroup$
2
$\begingroup$

Call value_counts() and then plot the returned series like this:

df["col_name"].value_counts().plot.pie()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.