This is a question from someone who tries to answer questions about pandas dataframes. Consider a question with a given dataset which is just the visualization (not the actual code), for example:
numbers letters dates all
0 1 a 20-10-2020 NaN
1 2 b 21-10-2020 b
2 3 c 20-11-2020 4
3 4 d 20-10-2021 20-10-2020
4 5 e 10-10-2020 3.14
Is it possible to quickly import this in python as a dataframe or as a dictionary? So far I copied the given text and transformed it to a dataframe by making strings (adding ''
) and so on.
I think there are two 'solutions' for this:
- Make a function that given the text as input, it somehow transforms it to a dataframe.
- Use some function in the text-editor (I use spyder) which can do this trick for us.
CodePudding user response:
read_clipboard
You can use pd.read_clipboard()
optionally with a separator (e.g. pd.read_clipboard('\s\s ')
if you have datetime strings or spaces in column names and columns are separated by at least two spaces):
- select text on the question and copy to clipboard (ctrl c/command-c)
- move to python shell or notebook and run
pd.read_clipboard()
Note that this doesn't work well on all platforms.
read_csv
io.StringIO
For more complex formats, combine read_csv
combined with io.StringIO
:
data = '''
numbers letters dates all
0 1 a 20-10-2020 NaN
1 2 b 21-10-2020 b
2 3 c 20-11-2020 4
3 4 d 20-10-2021 20-10-2020
4 5 e 10-10-2020 3.14
'''
import io
df = pd.read_csv(io.StringIO(data), sep='\s ')
df