Skip to content

Instantly share code, notes, and snippets.

@rockavoldy
Last active May 25, 2023 08:06
Show Gist options
  • Save rockavoldy/436901dd9d6b9294dec4e52413ac3cd5 to your computer and use it in GitHub Desktop.
Save rockavoldy/436901dd9d6b9294dec4e52413ac3cd5 to your computer and use it in GitHub Desktop.

Merge CSV with Pandas

import pandas
import csv

# read csv first
first_data = pandas.read_csv('first_data.csv')
second_data = pandas.read_csv('second_data.csv')

# delete column using drop and parameter axis=1, only keep what we need
first_data = first_data.drop(['Group Name'], axis=1)
second_data = second_data.drop(['Group Name'], axis=1)

# merge the csv with how join work
# https://www.geeksforgeeks.org/how-to-merge-two-csv-files-by-specific-column-using-pandas-in-python/
result = first_data.merge(second_data, on='Origin ID', how='inner')

# then create new file from result dataframe
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html
result.to_csv('result_data.csv', float_format=str, quoting=csv.QUOTE_ALL, index=None)
# or to make sure it's not a float
result.to_csv('result_data.csv', float_format='%.0f', quoting=csv.QUOTE_ALL, index=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment