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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With