Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas data from stdin

Is it possible to have stdin data go into a pandas DataFrame?

Currently I'm saving the data in an intermediate json file and then doing:

pandas.read_json('my_json_file.json')

but was wondering if it's possible to pipe the stdin directly in the python script. I found this: How to read from stdin or from a file if no data is piped in Python? but not sure how to do a line-by-line insertion in a pandas DF.

like image 433
Fra Avatar asked Aug 28 '13 18:08

Fra


People also ask

Which method allows us to read data from the standard input stdin?

Method 2:Read Input From stdin in Python using input() The input() can be used to take input from the user while executing the program and also in the middle of the execution.


1 Answers

Just use the sys.stdin as a file object (which it actually is) and pass it to pandas read_xy method.

$ cat test.py
import sys
import pandas as pd

df = pd.read_json(sys.stdin)
print df

$ cat data.json
{"a": [1,2,3,4], "b":[3,4,5,6]}

$ python test.py < data.json
   a  b
0  1  3
1  2  4
2  3  5
3  4  6
like image 185
Viktor Kerkez Avatar answered Oct 16 '22 18:10

Viktor Kerkez