|
| 1 | +--- |
| 2 | +Title: 'first()' |
| 3 | +Description: 'Returns the first non-null value from each group.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Data Structures' |
| 9 | + - 'Pandas' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-python-3' |
| 12 | + - 'paths/data-science' |
| 13 | +--- |
| 14 | + |
| 15 | +The **`first()`** method in pandas returns the first non-null value from each group created by a `groupby()` operation. It is commonly used to extract representative or initial entries for grouped data, such as the first transaction, first record, or earliest occurrence. |
| 16 | + |
| 17 | +## Syntax |
| 18 | + |
| 19 | +```pseudo |
| 20 | +DataFrameGroupBy.first(numeric_only=False, min_count=-1, skipna=True) |
| 21 | +``` |
| 22 | + |
| 23 | +**Parameters:** |
| 24 | + |
| 25 | +- `numeric_only` (bool, default `False`): Includes only float, int, and boolean columns in the operation. |
| 26 | +- `min_count` (int, default `-1`): Minimum number of valid values required to perform the operation. If fewer values are available, returns `NaN`. |
| 27 | +- `skipna` (bool, default `True`): Excludes `NA`/`null` values. If all entries are `NA`, the result is `NA`. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +Returns a Series or [DataFrame](https://www.codecademy.com/resources/docs/pandas/dataframe) containing the first non-null value from each group. |
| 32 | + |
| 33 | +## Example 1 |
| 34 | + |
| 35 | +In this example, the method retrieves the first entry of each department from the DataFrame: |
| 36 | + |
| 37 | +```py |
| 38 | +import pandas as pd |
| 39 | + |
| 40 | +data = { |
| 41 | + 'Department': ['HR', 'Finance', 'HR', 'IT', 'Finance', 'IT'], |
| 42 | + 'Employee': ['Alice', 'Bob', 'Charlie', 'David', 'Eva', 'Frank'], |
| 43 | + 'Salary': [50000, 60000, 52000, 58000, 61000, 59000] |
| 44 | +} |
| 45 | + |
| 46 | +df = pd.DataFrame(data) |
| 47 | +result = df.groupby('Department').first() |
| 48 | +print(result) |
| 49 | +``` |
| 50 | + |
| 51 | +The output of this code is: |
| 52 | + |
| 53 | +```shell |
| 54 | + Employee Salary |
| 55 | +Department |
| 56 | +Finance Bob 60000 |
| 57 | +HR Alice 50000 |
| 58 | +IT David 58000 |
| 59 | +``` |
| 60 | + |
| 61 | +## Example 2 |
| 62 | + |
| 63 | +In this example, the method extracts the first available record for each city group while skipping missing values: |
| 64 | + |
| 65 | +```py |
| 66 | +import pandas as pd |
| 67 | +import numpy as np |
| 68 | + |
| 69 | +data = { |
| 70 | + 'City': ['Delhi', 'Delhi', 'Mumbai', 'Mumbai', 'Chennai', 'Chennai'], |
| 71 | + 'Temperature': [32, np.nan, 30, 29, np.nan, 27], |
| 72 | + 'Humidity': [80, 82, 75, 78, 70, 72] |
| 73 | +} |
| 74 | + |
| 75 | +df = pd.DataFrame(data) |
| 76 | +result = df.groupby('City').first() |
| 77 | +print(result) |
| 78 | +``` |
| 79 | + |
| 80 | +The output of this code is: |
| 81 | + |
| 82 | +```shell |
| 83 | + Temperature Humidity |
| 84 | +City |
| 85 | +Chennai 27.0 70 |
| 86 | +Delhi 32.0 80 |
| 87 | +Mumbai 30.0 75 |
| 88 | +``` |
| 89 | + |
| 90 | +## Codebyte Example |
| 91 | + |
| 92 | +In this example, the method finds the first purchase record for each customer, showing how it can be applied to analyze grouped transaction data: |
| 93 | + |
| 94 | +```codebyte/python |
| 95 | +import pandas as pd |
| 96 | +
|
| 97 | +data = { |
| 98 | + 'Customer': ['Alice', 'Alice', 'Bob', 'Charlie', 'Charlie', 'Charlie'], |
| 99 | + 'Purchase': ['Shoes', 'Bag', 'Watch', 'Shirt', 'Shoes', 'Hat'], |
| 100 | + 'Amount': [120, 80, 150, 200, 90, 50] |
| 101 | +} |
| 102 | +
|
| 103 | +df = pd.DataFrame(data) |
| 104 | +result = df.groupby('Customer').first() |
| 105 | +print(result) |
| 106 | +``` |
| 107 | + |
| 108 | +## Frequently Asked Questions |
| 109 | + |
| 110 | +### 1. What does `groupby().first()` do? |
| 111 | + |
| 112 | +`groupby().first()` returns the first non-null record from each group in a grouped pandas object. |
| 113 | + |
| 114 | +### 2. How do you get the first in a pandas group? |
| 115 | + |
| 116 | +By applying the `first()` method after using `groupby()`, such as `df.groupby('Column').first()`. |
| 117 | + |
| 118 | +### 3. How to print the first 10 rows of a pandas DataFrame? |
| 119 | + |
| 120 | +Use the `head(10)` method, which displays the first 10 rows of the DataFrame. |
0 commit comments