This must be obvious, but I couldn't find an easy solution.
I have pandas DataFrame like this:
actual | predicted
------ + ---------
Apple | Apple
Apple | Apple
Apple | Banana
Banana | Orange
Orange | Apple
I want this:
| Apple | Banana | Orange
------ + ------- + ------- + -------
Apple | 2 | 1 | 0
Banana | 0 | 0 | 1
Orange | 1 | 0 | 0
Get Number of Rows in DataFrame You can use len(df. index) to find the number of rows in pandas DataFrame, df. index returns RangeIndex(start=0, stop=8, step=1) and use it on len() to get the count.
To get the number of rows, and columns we can use len(df. axes[]) function in Python.
You can use groupby
with aggregating size
and unstack
MultiIndex
:
df = df.groupby(['actual','predicted']).size().unstack(fill_value=0)
print (df)
predicted Apple Banana Orange
actual
Apple 2 1 0
Banana 0 0 1
Orange 1 0 0
Another solution with crosstab
:
df = pd.crosstab(df.actual, df.predicted)
print (df)
predicted Apple Banana Orange
actual
Apple 2 1 0
Banana 0 0 1
Orange 1 0 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With