python - How do I turn this json object into a panda dataframe? -
i have csv file turn json can turn panda data-frame.
here code,
def create_png(): f = open('sticks.csv', 'r') reader = csv.dictreader( f, fieldnames = ("xstk", "stk") ) out = json.dumps( [row row in reader ] ) print out df = dataframe([ #todo ]) the line 'print out' prints out "[{"xstk": "1", "stk": "0"}, {"xstk": "0", "stk": "1"}, {"xstk": "1", "stk": "0"}]"
so can put #todo make code run , turn simple 2 column dataframe? have looked @ create pandas dataframe json objects complicated example.
simply read string of json in directly read_json:
in [11]: pd.read_json('[{"xstk": "1", "stk": "0"}, {"xstk": "0", "stk": "1"}, {"xstk": "1", "stk": "0"}]') out[11]: stk xstk 0 0 1 1 1 0 2 0 1 however, if have csv should read in directly using read_csv:
df = pd.read_csv('sticks.csv', names=["xstk", "stk"])
Comments
Post a Comment