a = np.array(["foo", "foo", "foo", "foo", "bar", "bar",
"bar", "bar", "foo", "foo", "foo"], dtype=object)
b = np.array(["one", "one", "one", "two", "one", "one",
"one", "two", "two", "two", "one"], dtype=object)
data = {'A': a, 'B': b}
df = pd.DataFrame(data)
df['C'] = 1
table = pd.pivot_table(df, values='C',index=['A'], columns=['B'], aggfunc="count")
table.to_excel("table.xlsx")
print('Name: ',table.columns.name)
print()
print('Table: ')
print(table)
Name: B
Table:
B one two
A
bar 3 1
foo 4 3
This is my code so far. My goal is to create several pivot tables each with data that looks similar to this example and export them to excel. I would like to, if possible keep the DataFrame.columns.name attribute during the export. The current export looks like this:
| A | one | two |
|---|---|---|
| bar | 3 | 1 |
| foo | 4 | 3 |
and if possible I would like an export that looks like this:
| B | one | two |
|---|---|---|
| A | ||
| bar | 3 | 1 |
| foo | 4 | 3 |