Pasting data into a pandas dataframe

2024/10/17 21:16:30

This is the same question as this question, which is marked as a duplicate of this one.
The problem, and the reason I'm still asking, is that the solution provided (using pandas.read_clipboard()) currently doesn't work.
I use Python 3.5 and whenever I try to copy data from the clipboard I get an error :

"It seems the kernel died unexpectedly" which seems to be restricted to 3.5.

Any other way to get a simple dataframe like the one below in a pandas dataframe without resorting to manually typing it or downgrading Python?

   c1  c2  c3  c4
0   1   2   2   1
1   1   3   2   3
2   3   4   4   3
3   4   5   6   5
4   5   6   9   7

in R I would be usingread.table() with the text= argument

Thank you for your help.

Answer

You can use io.StringIO with pd.read_table:

import io
s = '''c1  c2  c3  c4
0   1   2   2   1
1   1   3   2   3
2   3   4   4   3
3   4   5   6   5
4   5   6   9   7
'''pd.read_table(io.StringIO(s), delim_whitespace=True)
Out: c1  c2  c3  c4
0   1   2   2   1
1   1   3   2   3
2   3   4   4   3
3   4   5   6   5
4   5   6   9   7
https://en.xdnf.cn/q/73051.html

Related Q&A

Add graph description under graph in pylab [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Is there a way of drawing a caption box in matplotlib Is it possible to add graph description under graph in pylab?Lets s…

Using Python textwrap.shorten for string but with bytes width

Id like to shorten a string using textwrap.shorten or a function like it. The string can potentially have non-ASCII characters. Whats special here is that the maximal width is for the bytes encoding of…

How to create a transparent mask in opencv-python

I have sign (signs with arbitrary shape) images with white background and I want to get an image of the sign with transparent background. I have managed to create a mask and apply it to the image and t…

Variables with dynamic shape TensorFlow

I need to create a matrix in TensorFlow to store some values. The trick is the matrix has to support dynamic shape.I am trying to do the same I would do in numpy: myVar = tf.Variable(tf.zeros((x,y), va…

python protobuf cant deserialize message

Getting started with protobuf in python I face a strange issue:a simple message proto definition is:syntax = "proto3"; package test;message Message {string message = 1;string sender = 2; }gen…

Seaborn: title and subtitle placement

H all,Id like to create a scatterplot with a title, subtitle, colours corresponding to a specific variable and size corresponding to another variable. I want to display the colour legend but not the si…

Calculate a rolling regression in Pandas and store the slope

I have some time series data and I want to calculate a groupwise rolling regression of the last n days in Pandas and store the slope of that regression in a new column.I searched the older questions an…

Python read microphone

I am trying to make python grab data from my microphone, as I want to make a random generator which will use noise from it. So basically I dont want to record the sounds, but rather read it in as a da…

How to tell pytest-xdist to run tests from one folder sequencially and the rest in parallel?

Imagine that I have test/unit/... which are safe to run in parallel and test/functional/... which cannot be run in parallel yet.Is there an easy way to convince pytest to run the functional ones sequen…

PyPDF4 - Exported PDF file size too big

I have a PDF file of around 7000 pages and 479 MB. I have create a python script using PyPDF4 to extract only specific pages if the pages contain specific words. The script works but the new PDF file,…