I want to get a list of columns names from a pandas pivot table. When I am printing columns of a table i don't get the first one (like in the result below). How can I get a list of all columns?
import pandas as pd
import numpy as np
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]})
df_table = pd.pivot_table(df,index=['A'], values='D', columns=['C'], aggfunc=np.sum)
print(df_table)
print(list(df_table.columns))
the result:
C large small
A
bar 11 11
foo 4 7
['large', 'small']