# How to read Panda's DataFrames from json file?

**URL:** <https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [January 30, 2023, 12:40am UTC](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749 "2023-01-30T00:40:21Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [January 30, 2023, 4:54am UTC](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749/21 "2023-01-30T04:54:52Z")

</div>

I’m not sure you can by changing the `for` loop, but you can use it in the last line like  
`df = permutedims(reduce(vcat,rows), :KeyID)`  
although I’m sure that’s occurred to you.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 30, 2023, 8:36am UTC](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749/22 "2023-01-30T08:36:11Z")

</div>

```julia
df=DataFrame(keys=[k for k in keys(jsobj.Var)]) # Get list of row keys from anywhere. 
# "Var" is the first easy symbol to write 
:grinning:

# Then take all the values to put in the column
for sym in keys(jsobj)
    df[:,sym]=[getproperty(jsobj,sym)[k] for k in keys(jsobj[sym])]
end
 

```

put in a more general form

```julia
df=DataFrame(keys=collect(keys(getproperty(jsobj,first(keys(jsobj))))))

foreach(s->df[:,s]=collect(values(getproperty(jsobj,s))), keys(jsobj))

```

---

<div class="post-metadata">

**Author:** ![sERRa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/serra/32/46358_2.png) [@sERRa](https://discourse.julialang.org/u/sERRa)\
**Post date:** [January 30, 2023, 6:12pm UTC](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749/23 "2023-01-30T18:12:08Z")

</div>

> [@rocco\_sprmnt21](#):
>
> ```julia
> df=DataFrame(keys=collect(keys(getproperty(jsobj,first(keys(jsobj))))))
> 
> foreach(s->df[:,s]=collect(values(getproperty(jsobj,s))), keys(jsobj))
> 
> ```

Perfect!

---

<div class="post-metadata">

**Author:** ![sERRa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/serra/32/46358_2.png) [@sERRa](https://discourse.julialang.org/u/sERRa)\
**Post date:** [January 30, 2023, 6:15pm UTC](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749/24 "2023-01-30T18:15:45Z")

</div>

How would you write it in order to have it transposed? (i.e. the columns changed to rows)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 30, 2023, 10:35pm UTC](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749/25 "2023-01-30T22:35:02Z")

</div>

You mean this?

```julia
dff=DataFrame(keys=collect(keys(jsobj)))
dfr=DataFrame(NamedTuple.(values(jsobj)))
[dff dfr]

```

or

```julia
hcat(DataFrame.([(keys=collect(keys(jsobj)),), NamedTuple.(values(jsobj))])...)

```

---

<div class="post-metadata">

**Author:** ![sERRa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/serra/32/46358_2.png) [@sERRa](https://discourse.julialang.org/u/sERRa)\
**Post date:** [January 31, 2023, 3:30am UTC](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749/26 "2023-01-31T03:30:05Z")

</div>

> [@rocco\_sprmnt21](#):
>
> ```julia
> dff=DataFrame(keys=collect(keys(jsobj)))
> dfr=DataFrame(NamedTuple.(values(jsobj)))
> [dff dfr]
> 
> ```

Exactly! Thank you.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [February 1, 2023, 10:40am UTC](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749/27 "2023-02-01T10:40:30Z")

</div>

I’ve been meaning to make a package for this for ages, and this post has prompted me to start one: [GitHub - cjdoris/PandasIO.jl: Read/write Pandas dataframes](https://github.com/cjdoris/PandasIO.jl)

Example:

```julia
julia> using PandasIO, DataFrames

julia> df = PandasIO.read_json("00_stats.json", DataFrame)
32×105 DataFrame
 Row │ dRBYd dTD/RZ dYd/G PuntRt ToPLead Con% ⋯
     │ Float64 Float64 Float64 Float64 Float64 Float64 ⋯
─────┼──────────────────────────────────────────────────────────────────────
   1 │ -0.00361946 2.02676 -0.20186 -0.712688 1.32705 1.24077 ⋯
   2 │ -1.18115 0.29747 -0.664647 1.71805 -0.551793 0.23976
   3 │ 0.826444 1.24938 2.45037 1.8372 0.611967 0.23976
   4 │ -0.177354 -1.73326 -0.929148 0.288203 1.02793 1.64227
   5 │ -0.988114 -0.511649 -0.657791 0.574172 1.00456 1.09227 ⋯
   6 │ 1.54069 -0.0674266 1.46255 0.550341 -1.78566 -1.50924
   7 │ -0.351088 -0.495784 -0.496853 -1.26079 -0.103114 -0.75573
  ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱
  26 │ 0.208722 -0.162617 0.969991 1.98018 -1.40241 -0.25523
  27 │ 1.0967 -0.210212 -0.342591 -0.402888 -0.304085 0.23976 ⋯
  28 │ 2.29353 -0.654435 -0.109844 0.717156 0.541861 0.74026
  29 │ -0.814379 0.884479 0.431427 0.0498956 0.471755 0.74026
  30 │ -0.505518 -0.860681 -1.2411 -0.212242 -0.308759 -0.50823
  31 │ -0.486215 -1.01933 -1.2005 0.598002 -1.13601 -0.25523 ⋯
  32 │ -2.04982 -0.130887 -1.56045 1.19377 2.07485 1.74127
                                             100 columns and 18 rows omitted

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 1, 2023, 1:42pm UTC](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749/28 "2023-02-01T13:42:45Z")

</div>

Also see [GitHub - JuliaPy/Pandas.jl: A Julia front-end to Python's Pandas package.](https://github.com/JuliaPy/Pandas.jl) especially if you need some edgecase exactly like it is in `pandas`.

[Previous page](https://discourse.julialang.org/t/how-to-read-pandas-dataframes-from-json-file/93749.md?page=1)
