join
The join
command combines data from two files based on a common column.
Syntax
qsv join <common_column> <file1> <common_column> <file2> [<options>]
Description
The join
command allows you to merge data from two files based on a common column. This is particularly useful when you need to combine information from different sources.
Options
-L, --left
: Perform a left join-R, --right
: Perform a right join-I, --inner
: Perform an inner join-F, --full
: Perform a full outer join
Interactive Example
qsv join -R CountryCode olympics2024.csv CountryCode countries.csv \
| qsv select Rank,Country,Gold,Continent \
| qsv table
Basic Join
Join two CSV files based on the CountryCode
column:
qsv join CountryCode olympics2024.csv CountryCode countries.csv | qsv select Rank,Country,Gold,Continent | qsv table
Output:
Rank Country Gold Continent
1 United States 39 North America
2 China 38 Asia
3 Japan 27 Asia
4 Great Britain 22 Europe
5 ROC 20 Europe
Left Join
Perform a left join:
qsv join -L CountryCode olympics2024.csv CountryCode countries.csv | qsv select Rank,Country,Gold,Continent | qsv table
This will include all rows from the first file (olympics2024.csv
) and only the matching rows from the second file (countries.csv
).
Right Join
Perform a right join:
qsv join -R CountryCode olympics2024.csv CountryCode countries.csv | qsv select Rank,Country,Gold,Continent | qsv table
This will include all rows from the second file (countries.csv
) and only the matching rows from the first file (olympics2024.csv
).
Inner Join
Perform an inner join:
qsv join -I CountryCode olympics2024.csv CountryCode countries.csv | qsv select Rank,Country,Gold,Continent | qsv table
This will include only the rows where there is a match in both files.
Full Outer Join
Perform a full outer join:
qsv join -F CountryCode olympics2024.csv CountryCode countries.csv | qsv select Rank,Country,Gold,Continent | qsv table
This will include all rows from both files, with null values where there is no match.
Common Use Cases
- Combining data from different sources
- Merging datasets based on a common identifier
- Enriching data with additional information
Tips
- Verify your joins by inspecting the output
- Use the appropriate join type based on your data and requirements