Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pasting data into a pandas dataframe

Tags:

python

pandas

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.

like image 505
Haboryme Avatar asked Dec 01 '16 15:12

Haboryme


People also ask

How do I add data to an existing DataFrame pandas?

append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value.

How do you copy and paste a DataFrame in Python?

to_clipboard() By default, the excel parameter is set to True , and the contents of DataFrame are copied to the clipboard separated by TAB \t . It can be pasted directly into spreadsheets such as Excel and Numbers. If excel=False , the string displayed by print(df) is copied to the clipboard.

How do I copy data from one DataFrame to another in Python?

Pandas DataFrame copy() Method The copy() method returns a copy of the DataFrame. By default, the copy is a "deep copy" meaning that any changes made in the original DataFrame will NOT be reflected in the copy.

How do I add to an existing data frame?

Dataframe append syntax Using the append method on a dataframe is very simple. You type the name of the first dataframe, and then . append() to call the method. Then inside the parenthesis, you type the name of the second dataframe, which you want to append to the end of the first.

What is a Dataframe in pandas?

Picture by Tyler Franta, Unsplash. Pandas allows you to import data from a wide range of data sources directly into a dataframe. These can be static files, such as CSV, TSV, fixed width files, Microsoft Excel, JSON, SAS and SPSS files, as well as a range of popular databases, such as MySQL, PostgreSQL and Google BigQuery.

How to get Dataframe from clipboard in pandas?

The read_clipboard () method of Pandas creates a DataFrame from data copied to the clipboard. It reads text from the clipboard and passes it to read_csv (), which then returns a parsed DataFrame object. More From Parul Pandey 10 Python Image Manipulation Tools You Can Use Today

How do I call a pandas function from a CSV file?

When importing the Pandas package the convention is to use the command import pandas as pd which allows you to call Pandas functions by prefixing them with pd. instead of pandas.. Comma Separated Value or CSV files are likely to be the file format you encounter most commonly in data science.

What do I need to import a pandas file?

You’ll also need the Datetime package. When importing the Pandas package the convention is to use the command import pandas as pd which allows you to call Pandas functions by prefixing them with pd. instead of pandas.. Comma Separated Value or CSV files are likely to be the file format you encounter most commonly in data science.


1 Answers

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
like image 102
ayhan Avatar answered Sep 17 '22 04:09

ayhan