Jake Jake - 1 month ago 7
Python Question

How do you use pandas.DataFrame columns as index, columns, and values?

I can't seem to figure out how to ask this question in a searchable way, but I feel like this is a simple question.

Given a pandas Dataframe object, I would like to use one column as the index, one column as the columns, and a third column as the values.

For example:

a b c
0 1 dog 2
1 1 cat 1
2 1 rat 6
3 2 cat 2
4 3 dog 1
5 3 cat 4





I would like to user column 'a' as my index values, column 'b' as my columns, and column 'c' as the values for each row/column and fill with 0 for missing values (if possible). For example...

dog cat rat
1 2 1 6
2 0 2 0
3 1 4 0


This would be an 'a' by 'b' matrix with 'c' as the filling values

Answer

It's (almost) exactly as you phrase it:

df.pivot_table(index="a", columns="b", values="c", fill_value=0)

gives

b  cat  dog  rat
a               
1    1    2    6
2    2    0    0
3    4    1    0

HTH

Comments