I am trying to compute group frequencies using this multi-indexed DataFrame:
In [1]: frame
Out[1]:
position values
idmajor idminor
22 2 6 A
4 1 7 B
11 1 7 C
23 1 7 B
1 1 8 C
9 8 C
4 1 8 C
1 8 C
Here is my code and error:
In [2]: pd.crosstab(frame.position, frame.values)
Out[2]: Exception: Data must be 1-dimensional
Here is what I am trying to output:
Out[2]:
categories A B C
values
6 1 0 0
7 0 2 1
8 0 0 4
I'm reading through Python for Data Analysis and have just started with the pandas
library. I am struggling to find answers or examples that will give me a greater understanding to this problem. Any advice is greatly appreciated!
EDIT: Due to the multi-index of the DataFrame I was misled by the Exception. The multi-index nature of this frame has nothing to do with the Exception.
In the documentation, pd.DataFrame.values is reserved through pandas in the Python namespace so I was referencing a function of my data column and not a column label like I thought I was!
The dot notation pd.crosstab(frame.position, frame.values)
returns the error Exception: Data must be 1-dimensional
whereas the dictionary notation pd.crosstab(frame['position'], frame['values'])
returns success!
In [2]: pd.crosstab(frame['position'], frame['values'])
Out[2]:
categories A B C
values
6 1 0 0
7 0 2 1
8 0 0 4
To avoid future confusion my solution and answer will be to just change the name of my DataFrame column values
to categories
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.