multi-index

Select rows in pandas MultiIndex DataFrame

余生颓废 提交于 2019-11-26 01:23:56
问题 What are the most common pandas ways to select/filter rows of a dataframe whose index is a MultiIndex? Slicing based on a single value/label Slicing based on multiple labels from one or more levels Filtering on boolean conditions and expressions Which methods are applicable in what circumstances Assumptions for simplicity: input dataframe does not have duplicate index keys input dataframe below only has two levels. (Most solutions shown here generalize to N levels) Example input: mux = pd

Converting a Pandas GroupBy output from Series to DataFrame

若如初见. 提交于 2019-11-26 00:23:17
问题 I\'m starting with input data like this df1 = pandas.DataFrame( { \"Name\" : [\"Alice\", \"Bob\", \"Mallory\", \"Mallory\", \"Bob\" , \"Mallory\"] , \"City\" : [\"Seattle\", \"Seattle\", \"Portland\", \"Seattle\", \"Seattle\", \"Portland\"] } ) Which when printed appears as this: City Name 0 Seattle Alice 1 Seattle Bob 2 Portland Mallory 3 Seattle Mallory 4 Seattle Bob 5 Portland Mallory Grouping is simple enough: g1 = df1.groupby( [ \"Name\", \"City\"] ).count() and printing yields a GroupBy