From version 0.20.1 it is simplier:
Strings passed to DataFrame.groupby() as the by parameter may now reference either column names or index level names
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second']) df = pd.DataFrame({'A': [1, 1, 1, 1, 2, 2, 3, 3], 'B': np.arange(8)}, index=index) print (df) A B first second bar one 1 0 two 1 1 baz one 1 2 two 1 3 foo one 2 4 two 2 5 qux one 3 6 two 3 7 print (df.groupby(['second', 'A']).sum()) B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7
Related Posts:
- how to sort pandas dataframe from one column
- How to reset index in a pandas dataframe? [duplicate]
- Constructing pandas DataFrame from values in variables gives “ValueError: If using all scalar values, you must pass an index”
- How to iterate over rows in a DataFrame in Pandas
- pandas read_json: “If using all scalar values, you must pass an index”
- Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
- Writing a pandas DataFrame to CSV file
- Pandas DataFrame Groupby two columns and get counts
- How to show all columns’ names on a large pandas dataframe?
- ValueError: Unknown label type: ‘continuous’
- How to groupby based on two columns in pandas?
- How to deal with SettingWithCopyWarning in Pandas
- ImportError: No module named pandas
- TypeError: ‘Series’ objects are mutable, thus they cannot be hashed problemwith column
- How to replace NaN values by Zeroes in a column of a Pandas Dataframe?
- ValueError: Length of values does not match length of index | Pandas DataFrame.unique()
- Creating an empty Pandas DataFrame, then filling it?
- Python Pandas Counting the Occurrences of a Specific value
- Count unique values per groups with Pandas
- DataFrame constructor not properly called
- Convert pandas Series to DataFrame
- ImportError: Missing required dependencies [‘numpy’]
- How to iterate over rows in a DataFrame in Pandas
- Pandas group-by and sum
- Pandas: ValueError: cannot convert float NaN to integer
- ValueError: Length of values does not match length of index | Pandas DataFrame.unique()
- ‘DataFrame’ object has no attribute ‘sort’
- pandas: merge (join) two data frames on multiple columns
- Why do I get: “Length of values does not match length of index” error?
- why should I make a copy of a data frame in pandas
- ‘DataFrame’ object has no attribute ‘sort’
- Pandas, merging two dataframes on multiple columns, and multiplying result
- How to customize a scatter matrix to see all titles?
- What is dtype(‘O’), in pandas?
- How to read a .xlsx file using the pandas Library in iPython?
- Plot pie chart and table of pandas dataframe
- What is dtype(‘O’), in pandas?
- Pandas ‘count(distinct)’ equivalent
- ValueError: ‘object too deep for desired array’
- What does axis in pandas mean?
- How to take column-slices of dataframe in pandas
- AttributeError: Can only use .dt accessor with datetimelike values
- Combine two columns of text in pandas dataframe
- xlrd.biffh.XLRDError: Excel xlsx file; not supported
- Pandas: change data type of Series to String
- Normalize data in pandas
- Pandas – DataFrame object is not callable
- Selecting with complex criteria from pandas.DataFrame
- module ‘pandas’ has no attribute ‘rolling_mean’
- How do I combine two dataframes?
- Normalize columns of pandas data frame
- Count unique values using pandas groupby
- Normalize columns of pandas data frame
- Ignoring NaNs with str.containsv
- How to add an empty column to a dataframe?
- Pandas join issue: columns overlap but no suffix specified
- Count unique values per groups with Pandas [duplicate]
- How to apply a function to two columns of Pandas dataframe
- Convert categorical data in pandas dataframe
- No numeric types to aggregate – change in groupby() behaviour?
- Loading a file with more than one line of JSON into Pandas
- Solution for SpecificationError: nested renamer is not supported while agg() along with groupby()
- ValueError: Expected object or value when reading json as pandas dataframe
- Insert a row to pandas dataframe
- How to save a Seaborn plot into a file
- Subtract two columns in dataframe
- How to check if a column exists in Pandas
- pandas DataFrame: replace nan values with average of columns
- Pandas error “Can only use .str accessor with string values”
- Pandas split DataFrame by column value
- How to get row number in dataframe in Pandas?
- AttributeError: ‘Series’ object has no attribute ‘reshape’
- How to convert column with dtype as object to string in Pandas Dataframe
- datetime to string with series in pandas
- Coalesce values from 2 columns into a single column in a pandas dataframe
- Unknown format code ‘f’ for object of type ‘str’- Folium
- Find the unique values in a column and then sort them
- pandas groupby sort within groups
- Pandas: sum up multiple columns into one column without last column
- Convert Pandas Column to DateTime
- if else function in pandas dataframe
- Python: Pandas pd.read_excel giving ImportError: Install xlrd >= 0.9.0 for Excel support
- Read data (.dat file) with Pandas
- Boolean Series key will be reindexed to match DataFrame index
- Converting Pandas dataframe into Spark dataframe error
- Must have equal len keys and value when setting with an iterable
- Difference between data type ‘datetime64[ns]’ and ‘
- Merging two DataFrames
- Pandas dataframe groupby plot
- pandas: multiple conditions while indexing data frame – unexpected behavior
- How to print a specific row of a pandas DataFrame?
- Logical operators for Boolean indexing in Pandas
- How to convert .sav file into csv file
- How to update Pandas from Anaconda and is it possible to use eclipse with this last
- vectorize conditional assignment in pandas dataframe
- Convert list of dictionaries to a pandas DataFrame
- Python Pandas : pivot table with aggfunc = count unique distinct
- Compare two columns using pandas
- AttributeError: ‘Series’ object has no attribute ‘split’ error in sending emails
- alueError: ordinal must be >= 1