Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda join() | s1
/
Track DA with Py - Data Manipulation with pandas

bookjoin()

Deslize para mostrar o menu

The join() method in pandas allows you to combine two DataFrame objects based on their indexes. Unlike the merge() method, which typically matches rows using one or more columns (keys), join() aligns rows by their indexes, making it ideal when your data is already indexed in a compatible way. This method is especially convenient for adding columns from one DataFrame to another when their indexes represent the same entities, such as dates, IDs, or categories.

123456789101112131415
import pandas as pd # Create two DataFrames with indexes representing employee IDs df_left = pd.DataFrame({ "name": ["Alice", "Bob", "Charlie"], "salary": [70000, 80000, 90000] }, index=[101, 102, 103]) df_right = pd.DataFrame({ "department": ["HR", "Engineering", "Marketing"] }, index=[101, 102, 104]) # Join df_right to df_left using their indexes (default is left join) result = df_left.join(df_right) print(result)
copy

The join() method offers several parameters to control its behavior. The how parameter determines the type of join to perform: "left" (default), "right", "outer", or "inner". This controls which indexes are included in the result. The lsuffix and rsuffix parameters are used to add suffixes to overlapping column names from the left and right DataFrame, respectively. This helps prevent column name collisions when both DataFrame objects have columns with the same name.

1234567891011121314
# Two DataFrames with overlapping column names df_left = pd.DataFrame({ "name": ["Alice", "Bob"], "age": [25, 30] }, index=[1, 2]) df_right = pd.DataFrame({ "age": [28, 35], "city": ["New York", "Chicago"] }, index=[1, 3]) # Join with suffixes to distinguish overlapping 'age' columns result = df_left.join(df_right, lsuffix="_left", rsuffix="_right", how="outer") print(result)
copy
question mark

Which situation is best suited for using the join() method instead of merge() in pandas?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 28

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 28
some-alt