I am using pandas pivot table option in the following way:
df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
... "bar", "bar", "bar", "bar"],
... "B": ["one", "one", "one", "two", "two",
... "one", "one", "two", "two"],
... "C": ["small", "large", "large", "small",
... "small", "large", "small", "small",
... "large"],
... "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
... "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],
... aggfunc={'D': 'count',
... 'E': np.mean})
the outcome is:
D E
A C
bar large 2 7.500000
small 2 8.500000
foo large 2 4.500000
small 3 4.333333
How can I change the pd.pivot_table
command that I have:
Nr.of D E
A C
bar large 2 7.500000
small 2 8.500000
foo large 2 4.500000
small 3 4.333333
Followed by this post it is possible to rename it afterwards pandas pivot table rename columns , but with some more columns it can be tricky to rename it, is it possible to change the name of the values=['D', 'E']
directly in the pd.pivot_table
line?