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
- How to use filter, map, and reduce in Python 3
- how to sort pandas dataframe from one column
- Is there a way to create multiline comments in Python?
- ‘pip’ is not recognized as an internal or external command
- Renaming column names in Pandas
- How to reset index in a pandas dataframe? [duplicate]
- What is the purpose of the word ‘self’?
- Python- Robot Framework Rebot Using List
- How to update/upgrade a package using pip?
- How can I remove a specific item from an array?
- Behaviour of increment and decrement operators in Python
- Convert bytes to a string
- Python vs Cpython
- How do I update\upgrade pip itself from inside my virtual environment?
- Python integer incrementing with ++ [duplicate]
- Replacing instances of a character in a string
- Changing one character in a string
- What is the use of “assert” in Python?
- How can I represent an ‘Enum’ in Python?
- Delete a column from a Pandas DataFrame
- IndexError: too many indices for array
- IndexError: too many indices for array
- numpy array: IndexError: too many indices for array
- How to deal with SettingWithCopyWarning in Pandas
- Python3 – ModuleNotFoundError: No module named ‘numpy’
- How to deal with SettingWithCopyWarning in Pandas
- How do I specify new lines on Python, when writing on files?
- What is the purpose of the return statement?
- 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”
- How to iterate over rows in a DataFrame in Pandas
- Relative imports – ModuleNotFoundError: No module named x
- bash: pip: command not found
- What is the difference between importing matplotlib and matplotlib.pyplot?
- Using global variables in a function
- How do I check what version of Python is running my script?
- How to read a large file – line by line?
- Writing a pandas DataFrame to CSV file
- How to uninstall pip on OSX?
- How to delete a file or folder in Python?
- Converting integer to string in Python
- deleting file if it exists; python
- Reverse a string in Python
- Why am I seeing “TypeError: string indices must be integers”?
- Python for-in loop preceded by a variable
- Python Linked List
- What is the result of % in Python?
- ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
- How do I upgrade the Python installation in Windows 10?
- What does if __name__ == “__main__”: do?
- How to print without a newline or space
- Python’s equivalent of && (logical-and) in an if-statement
- Difference between del, remove, and pop on lists
- Python time.sleep() vs event.wait()
- Program to Unjumble Words on Python [closed]
- TypeError: ‘module’ object is not callable
- “inconsistent use of tabs and spaces in indentation”
- How can I install packages using pip according to the requirements.txt file from a local directory?
- Error: ‘int’ object is not subscriptable – Python
- Referring to the null object in Python
- How to measure elapsed time in Python?
- How to uninstall Anaconda completely from macOS
- How to overcome TypeError: unhashable type: ‘list’
- Python: Start and stop timer [duplicate]
- Does Python have a ternary conditional operator?
- python .replace() regex [duplicate]
- not all arguments converted during string formatting.. NO % variables
- How do I install opencv using pip?
- TypeError: ‘builtin_function_or_method’ object is not subscriptable
- How do I use raw_input in Python 3
- Python Variable Declaration
- How do I compare two strings in python?
- SyntaxError: “can’t assign to function call”
- Curve curvature in numpy
- Relative imports in Python 3
- Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
- ImportError: No module named sklearn.cross_validation
- ImportError: No module named sklearn.cross_validation
- How can I prevent the TypeError: list indices must be integers, not tuple when copying a python list to a numpy array?
- 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
- What are data classes and how are they different from common classes?
- How to write the Fibonacci Sequence?
- “pip install unroll”: “python setup.py egg_info” failed with error code 1
- Tensorflow import error: No module named ‘tensorflow’
- Python random function