[PYTHON] Where are you from

What happens when you read two data in Pandas and combine them?

--Reading data

import pandas as pd

sample001 = pd.read_excel("sample_excel_001.xlsx")
sample001.head()
89781cb2-5d55-73df-1acf-b17f433e7b07.jpeg
sample002 = pd.read_excel("sample_excel_002.xlsx")
sample002.head()
a66b2cc7-69c6-4bf2-dc4e-5f6e304a0578.jpeg

Two data were loaded into the data frame (sample001, sample002). It looks like you can combine the two data in the "data001" column.

--Data join

merge_data = pd.merge(sample001, sample002, on="data001", how="left")
merge_data.head()
e1d8f492-631d-fa42-493e-26e091c9c4ed.jpeg It's easy to see that "data001" is the join keyword string and "data003" is the sample002 data. I'm talking about "data002_x" and "data002_y". You're asking, "Where are you from?" (No, you really know? The data frame specified in the first argument of merge has x) I don't like to see it, so at least I want to be able to quickly understand where the data came from.

How to give your name

You can use the suffixes option to specify a string to add to the end of the new column name when the column name is duplicated except for the join key.

merge_data_new = pd.merge(sample001, sample002, on="data001", how="left", suffixes=[".sample001", ".sample002"])
merge_data_new.head()
a9c397c7-23ce-94aa-1a10-1af997cbe426.jpeg

Oh, now you can easily tell where the data came from!

Finally

I wish I could, but I wish I could add a string at the beginning instead of the end. (Because it can be SQL-like)

Recommended Posts

Where are you from
Checking if you are Sudoku
[Note] Where did you use async?
Are you having trouble with "pipenv.exceptions.ResolutionFailure"?
Ask you guys! Are you a programmer! !!
About the development environment you are using