You just need to use apply
on the groupby
object. I modified your example data to make this a little more clear:
import pandas from io import StringIO csv = StringIO("""index,A,B 0,1,0.0 1,1,3.0 2,1,6.0 3,2,0.0 4,2,5.0 5,2,7.0""") df = pandas.read_csv(csv, index_col='index') groups = df.groupby(by=['A']) print(groups.apply(lambda g: g[g['B'] == g['B'].max()]))
Which prints:
A B A index 1 2 1 6 2 4 2 7
Related Posts:
- Get statistics for each group (such as count, mean, etc) using pandas GroupBy?
- how to reset index pandas dataframe after dropna() pandas dataframe
- Reading an Excel file in python using pandas
- Selecting multiple columns in a Pandas dataframe
- The difference between comparison to np.nan and isnull()
- How to groupby based on two columns in pandas?
- Pandas group-by and sum
- Count unique values per groups with Pandas
- Pandas group-by and sum
- Pandas ‘count(distinct)’ equivalent
- Count unique values using pandas groupby
- Count unique values per groups with Pandas [duplicate]
- pandas groupby sort within groups
- Is there a way to create multiline comments in Python?
- ‘pip’ is not recognized as an internal or external command
- How can I remove a specific item from an array?
- Python integer incrementing with ++ [duplicate]
- Replacing instances of a character in a string
- How to deal with SettingWithCopyWarning in Pandas
- Python3 – ModuleNotFoundError: No module named ‘numpy’
- How to deal with SettingWithCopyWarning in Pandas
- What is the purpose of the return statement?
- How to iterate over rows in a DataFrame in Pandas
- How to read a large file – line by line?
- Writing a pandas DataFrame to CSV file
- deleting file if it exists; python
- Why am I seeing “TypeError: string indices must be integers”?
- Python Linked List
- What is the result of % in Python?
- How to print without a newline or space
- Python time.sleep() vs event.wait()
- Program to Unjumble Words on Python [closed]
- Error: ‘int’ object is not subscriptable – Python
- How to overcome TypeError: unhashable type: ‘list’
- Does Python have a ternary conditional operator?
- How do I install opencv using pip?
- How do I compare two strings in python?
- Curve curvature in numpy
- What are data classes and how are they different from common classes?
- Tensorflow import error: No module named ‘tensorflow’
- Python random function
- Converting NumPy array into Python List structure?
- How do you get the logical xor of two variables in Python?
- ValueError: could not convert string to float: id
- Pip freeze vs. pip list
- ImportError: No module named pandas. Pandas installed pip
- How can I use the apply() function for a single column?
- Convenient way to deal with ValueError: cannot reindex from a duplicate axis
- “Series objects are mutable and cannot be hashed” error
- Merging dataframes on index with pandas
- Convert Python dict into a dataframe
- re.sub erroring with “Expected string or bytes-like object”
- How do I select rows from a DataFrame based on column values?
- DataFrame constructor not properly called! error
- Python pandas groupby aggregate on multiple columns, then pivot
- How can I one hot encode in Python?
- Python Pandas – Missing required dependencies [‘numpy’] 1
- Python Package bt: Bool index error
- pandas: filter rows of DataFrame with operator chaining
- Pandas how to use pd.cut()
- How to Read .txt in Pandas
- Get total of Pandas column
- What is key=lambda
- Type error: cannot convert the series to
- How to check whether a pandas DataFrame is empty?
- Convert columns to string in Pandas
- Change column type in pandas
- Python: pandas merge multiple dataframes
- How to load a tsv file into a Pandas DataFrame?
- How to count the NaN values in a column in pandas DataFrame
- Converting between datetime and Pandas Timestamp objects
- Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?
- Convert pandas data frame to series
- Get first row value of a given column
- Selecting/excluding sets of columns in pandas
- How to add title to seaborn boxplot
- How to filter Pandas dataframe using ‘in’ and ‘not in’ like in SQL
- Deleting DataFrame row in Pandas based on column value
- Pandas long to wide reshape, by two variables
- SettingWithCopyWarning even when using .loc[row_indexer,col_indexer] = value
- OSError: Initializing from file failed on csv in Pandas
- Meaning of @classmethod and @staticmethod for beginner?
- AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
- Python Pandas ValueError Arrays Must be All Same Length
- Plot correlation matrix using pandas
- Python/Django: log to console under runserver, log to file under Apache
- TensorFlow not found using pip
- Rename specific column(s) in pandas
- ImportError: No module named ‘xlrd’
- Moving Average Pandas
- What is key=lambda
- python pandas remove duplicate columns
- matplotlib: plot multiple columns of pandas data frame on the bar chart
- Concat DataFrame Reindexing only valid with uniquely valued Index objects
- Pandas unstack problems: ValueError: Index contains duplicate entries, cannot reshape
- Python: Convert timedelta to int in a dataframe
- Calculate weighted average using a pandas/dataframe
- Got continuous is not supported error in RandomForestRegressor
- ValueError: multiclass format is not supported
- ValueError: Must pass DataFrame with boolean values only