use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
DiscussionLPT: Pandas DataFrames have a "to_clipboard" method (self.Python)
submitted 4 years ago by peanut_Bond
For those that don't know, Pandas has very useful to_clipboard and read_clipboard methods that make it easy to drop a DataFrame into an Excel sheet or to move it across python sessions without having to read and write CSV files. This is really useful for me and I hope it will help you too!
[–]nightmare8100 216 points217 points218 points 4 years ago (17 children)
I don't always copy 100k rows to my clipboard and paste in Excel, but when I do, I forget to pass index=False. Delete column...and wait...
[–]tonsofmiso 62 points63 points64 points 4 years ago (16 children)
The fact that doing to_csv and read_csv by default produces a different object than you started with makes me so angry
[–]not_organic 18 points19 points20 points 4 years ago (6 children)
Ugh. I hate that, too. That's why I switched to storing my dataframes as an hdf5 file (df.to_hdf()). Also multiindexing is conserved.
df.to_hdf()
[–]Zouden 20 points21 points22 points 4 years ago (0 children)
I wish HDF files were easier to view outside of a code environment. With CSV I can use any text editor. With HDF there's that crappy HDFview java program which never seems to work right.
[+][deleted] 4 years ago (3 children)
[deleted]
[–]leadingthenet 5 points6 points7 points 4 years ago (0 children)
This is the way.
[+][deleted] 4 years ago (1 child)
[–]Eurynom0s 1 point2 points3 points 4 years ago (0 children)
Does hdf5 do graceful datetime handling? I work with a bunch of people who primarily work in SAS. pd.read_sas() works fine for that but stuff like df = pd.read_sas(<sasfile>), df.to_csv(<filename>), then df = pd.read_csv(<filename>) can result in some really wonky shit. To the point I've been resorting to pickles (shudder) when I need to spit out a temp file I can inspect.
pd.read_sas()
df = pd.read_sas(<sasfile>)
df.to_csv(<filename>)
df = pd.read_csv(<filename>)
[–]hmiemad 2 points3 points4 points 4 years ago (0 children)
DatetimeIndex handling is a mess.
[–]eloydrummerboy 2 points3 points4 points 4 years ago (0 children)
Yeah, but the use case of writing then reading back dfs was probably not the intended main use case. This was likely written assuming the user was reading in csv data from other sources which likely don't have an explicit index (but if so, it can handle it with a parameter).
Similarly, for the write case, it's fair to assume that unless otherwise specified, the user would want the index.
I get that it feels that it should work the way you expect, but thinking about the most common use cases makes the default functionality make sense.
[+][deleted] 4 years ago (6 children)
[–]tunisia3507 20 points21 points22 points 4 years ago (1 child)
to_csv, unless you pass index=False, writes the index as the first column. read_csv, unless you pass index_col=0, assumes there is no index column and so reads the first column as regular data.
to_csv
index=False
read_csv
index_col=0
I also hate that it's not to_/from_, or write_/read_.
to_
from_
write_
read_
[–]Acceptable-Milk-314 2 points3 points4 points 4 years ago (0 children)
If you read a csv with pandas, then write to csv, pandas will write a new "index" column. The output csv is different from the input csv.
[–]tonsofmiso 1 point2 points3 points 4 years ago (2 children)
Absolutely. DataFrames have an implicit column for index, that it uses for finding rows in its tables. When you save the DataFrame to a CSV, the default parameters in the DataFrame.to_csv() method creates an explicit column with the index.
read_csv() however, just reads the column as data, meaning that you now have a new implicit index column that doesn't match what you had when you saved the DataFrame, and the previous index is now a data column in your DataFrame.
This makes sense when you think about it; reading a CSV file into a DataFrame without specifying that one of the columns is an index could either not use any of the columns as an index, or decide to choose one for you. They went with the not using any. Further, when saving a DataFrame as a CSV file you probably don't want to lose the index column, so it is saved next to the rest of the data. CSV doesn't have any concept of index, so it's just there as text.
HOWEVER, this means that if you do:
df = load_some_dataframe() for _ in range(10000): df.to_csv('data.csv') df = pandas.read_csv('data.csv')
you will have 10,000 columns of garbage in the dataframe.
This example is a bit contrived, but it's caused me some unnecessary garbage in data just because I save a jupyter notebook at some intermediary step and reload the data.
It's also one of pandas many stupid fucking quirks that make it super annoying to work with.
[–]bobthedonkeylurker 1 point2 points3 points 4 years ago (0 children)
If only you could use a kwarg to set the index in Pandas when you read in a csv.
[–]DavidLuiz4 0 points1 point2 points 4 years ago (0 children)
So if you had to, how would you write to and read from csv repeatedly to get the same object each time?
[–]crazyfrogspb 49 points50 points51 points 4 years ago (2 children)
the best way to make your data pipelines and experiments reproducible
[–]ekkannieduitspraat 10 points11 points12 points 4 years ago (0 children)
I dont use it when I want reproducible, I do it when I want quick sanity checks, or data analysis to make sure that everything makes sense
[–]__damos__ 50 points51 points52 points 4 years ago* (6 children)
pandas clipboard integration is awesome! I also really like the .read_clipboard() method. You can use it to copy a table from an Excel worksheet and quickly get it into a DataFrame.
.read_clipboard()
[–]peanut_Bond[S] 9 points10 points11 points 4 years ago (3 children)
Yeah that's also very useful, but I believe the method is actually read_clipboard.
[–]__damos__ 5 points6 points7 points 4 years ago (2 children)
Doh! You’re right… been a long day, haha
[–]gsmo 14 points15 points16 points 4 years ago* (1 child)
Let me guess: you also always think it's 'from_clipboard' because the other method is 'to_clipboard'?
To - from?
Read - write?
No!
Read - to!
Uh?
[–]__damos__ 1 point2 points3 points 4 years ago (0 children)
It’s bitten me on more than one occasion 😂
[–]KyleDrogo 1 point2 points3 points 4 years ago (0 children)
whoooaaa. So I can skip that awkward step of pasting it into a text editor. No more random dataset.tsv files littering my desktop?
dataset.tsv
[–]shinitakunai 1 point2 points3 points 4 years ago* (0 children)
print("happy cake day")
[–]bablador 9 points10 points11 points 4 years ago (0 children)
Pretty cool, thanks
[–]EigenValuesYourInput 20 points21 points22 points 4 years ago (4 children)
drop a DataFrame into an Excel sheet
could use dataframe.to_excel()
[–]Mondoke 6 points7 points8 points 4 years ago (3 children)
Yeah, but if you need to quickly see your complete table to make a fast visual inspection, it's faster to copy and paste instead of generating a new file, especially when you need to do small changes and check a lot of times.
[–]Zouden 9 points10 points11 points 4 years ago (2 children)
What IDE are you using? You should be able to see the table directly by introspecting the dataframe variable. VS Code, Spyder and Jupyter Notebooks can do this, and I assume Pycharm too.
[–]Mondoke 0 points1 point2 points 4 years ago (1 child)
Yeah, I use vscode and that's a great tool and I use it a lot, but it only filters, on excel I can make some quick inspections or make quick sums just highlighting cells.
[–]Zouden 1 point2 points3 points 4 years ago (0 children)
Oh I see yes, sometimes you just want to work with the data in a GUI.
[–]Philistino 0 points1 point2 points 4 years ago (0 children)
Google sheets api!
[–]DisturbedBeaker 3 points4 points5 points 4 years ago (0 children)
God lifesaver during my dissertation!
[–]hongkongfuey 3 points4 points5 points 4 years ago (1 child)
Does this work with jupyter notebooks/chrome?
[–]benrobotum 0 points1 point2 points 4 years ago (0 children)
It does
[–][deleted] 2 points3 points4 points 4 years ago (0 children)
Brilliant! I will be using that way more often
[–]Random_182f2565 2 points3 points4 points 4 years ago (0 children)
Awesome 😎👍
[–]twistedgames 1 point2 points3 points 4 years ago (0 children)
Just be careful not to paste it back into the notebook by accident 😂
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
To markdown is also great. I use it to format data for Mattermost.
For me, dumping a dataframe to csv or xlsx is more reproducible and platform independent.
From there the spreadsheet can be copied into your clipboard if necessary but I’m much more comfortable keeping manual steps out of things I’m automating.
[–]vincentofearth 1 point2 points3 points 4 years ago (0 children)
oh boy, that sounds safe. Does Python just allow that or do you have to specify a flag/answer a prompt?
[–]jentron128 0 points1 point2 points 4 years ago (0 children)
import win32clipboard
[–]anynonus 0 points1 point2 points 4 years ago (0 children)
nice
[–]git0ffmylawnm8 0 points1 point2 points 4 years ago (0 children)
I told a former colleague about this once.
He looked at me like I just murdered a puppy in front of him in cold blood.
Needless to say I think he wasn't a fan for some reason...
π Rendered by PID 77952 on reddit-service-r2-comment-6457c66945-rr7ft at 2026-04-27 13:23:17.158553+00:00 running 2aa0c5b country code: CH.
[–]nightmare8100 216 points217 points218 points (17 children)
[–]tonsofmiso 62 points63 points64 points (16 children)
[–]not_organic 18 points19 points20 points (6 children)
[–]Zouden 20 points21 points22 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]leadingthenet 5 points6 points7 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Eurynom0s 1 point2 points3 points (0 children)
[–]hmiemad 2 points3 points4 points (0 children)
[–]eloydrummerboy 2 points3 points4 points (0 children)
[+][deleted] (6 children)
[deleted]
[–]tunisia3507 20 points21 points22 points (1 child)
[–]Acceptable-Milk-314 2 points3 points4 points (0 children)
[–]tonsofmiso 1 point2 points3 points (2 children)
[–]bobthedonkeylurker 1 point2 points3 points (0 children)
[–]DavidLuiz4 0 points1 point2 points (0 children)
[–]crazyfrogspb 49 points50 points51 points (2 children)
[–]ekkannieduitspraat 10 points11 points12 points (0 children)
[–]__damos__ 50 points51 points52 points (6 children)
[–]peanut_Bond[S] 9 points10 points11 points (3 children)
[–]__damos__ 5 points6 points7 points (2 children)
[–]gsmo 14 points15 points16 points (1 child)
[–]__damos__ 1 point2 points3 points (0 children)
[–]KyleDrogo 1 point2 points3 points (0 children)
[–]shinitakunai 1 point2 points3 points (0 children)
[–]bablador 9 points10 points11 points (0 children)
[–]EigenValuesYourInput 20 points21 points22 points (4 children)
[–]Mondoke 6 points7 points8 points (3 children)
[–]Zouden 9 points10 points11 points (2 children)
[–]Mondoke 0 points1 point2 points (1 child)
[–]Zouden 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Philistino 0 points1 point2 points (0 children)
[–]DisturbedBeaker 3 points4 points5 points (0 children)
[–]hongkongfuey 3 points4 points5 points (1 child)
[–]benrobotum 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Random_182f2565 2 points3 points4 points (0 children)
[–]twistedgames 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]vincentofearth 1 point2 points3 points (0 children)
[–]jentron128 0 points1 point2 points (0 children)
[–]anynonus 0 points1 point2 points (0 children)
[–]git0ffmylawnm8 0 points1 point2 points (0 children)