Use pandas

How to import a .csv from your clipboard with Python pandas

How to import a .csv from your clipboard with Python pandas

Your colleague sends you a .csv file in slack and wants you to do some quick transformations on it.

Good news! You don't have to save these .csv files to your machine, get the file path, and load it into a pandas dataframe.

There's an easier way!

It's called pandas.read_clipboard()

How to use pandas read_clipboard()

For this example, your colleague sends you this .csv in slack with housing data. He'd like it sorted from largest to small by bedrooms.

homes_sorted.csv

Here is what the original .csv file looks like

AddressPriceBedrooms
992 Settled St823,0494
1506 Guido St784,0493
247 Fort St299,2383
132 Walrus Ave299,0012
491 Python St293,9234
4981 Anytown Rd199,0004
938 Zeal Rd148,3982
123 Main St99,0001

Import data using read_clipboard()

Copy .csv to clipboard here: homes_sorted.csv

import pandas as pd
# read_clipboard attempts to read whatever is in your
# clipboard in as a .csv file. It uses read_csv() under
# the hood.
df = pd.read_clipboard(delimiter=',')
# sort data by bedrooms
sorted_df = df.sort_values(by=["bedrooms"])
# export dataframe as .csv file
sorted_df.to_csv('homes_sorted_by_bedroom.csv', index=False)

homes_sorted_by_bedroom.csv

AddressPriceBedrooms
992 Settled St823,0494
491 Python St293,9234
4981 Anytown Rd199,0004
1506 Guido St784,0493
247 Fort St299,2383
132 Walrus Ave299,0012
938 Zeal Rd148,3982
123 Main St99,0001
Edit this page on GitHub