# Pandas dataframe convert to Array

**URL:** <https://discourse.julialang.org/t/pandas-dataframe-convert-to-array/72178>\
**Category:** New to Julia\
**Tags:** plotting, dataframes\
**Created:** [November 27, 2021, 6:43pm UTC](https://discourse.julialang.org/t/pandas-dataframe-convert-to-array/72178 "2021-11-27T18:43:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![libensvivit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/libensvivit/32/19006_2.png) [@libensvivit](https://discourse.julialang.org/u/libensvivit)\
**Post date:** [November 27, 2021, 6:43pm UTC](https://discourse.julialang.org/t/pandas-dataframe-convert-to-array/72178/1 "2021-11-27T18:43:08Z")

</div>

```julia
using Pandas
using Plots: plot

df = read_table("M.out/table.txt")

x = Array(df["# t (s)"])

plot(x, Array(df["mx ()"]))
plot!(x, Array(df["my ()"]))
plot!(x, Array(df["mz ()"]))

```

In this code is there a way to reconstruct `df` so I don’t have to wrap an Array constructor every time?

I mean how to make something like this work: `plot(x, df["mx ()"])`

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 27, 2021, 6:46pm UTC](https://discourse.julialang.org/t/pandas-dataframe-convert-to-array/72178/2 "2021-11-27T18:46:12Z")

</div>

Do you have to use pandas? Why not DataFrames?

---

<div class="post-metadata">

**Author:** ![libensvivit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/libensvivit/32/19006_2.png) [@libensvivit](https://discourse.julialang.org/u/libensvivit)\
**Post date:** [November 27, 2021, 6:54pm UTC](https://discourse.julialang.org/t/pandas-dataframe-convert-to-array/72178/3 "2021-11-27T18:54:18Z")

</div>

My table.txt file is a simulation file it writes like this:

```julia
# t (s)	mx ()	my ()	mz ()	E_total (J)
1e-09	0.9999239	0.008726204	0	-1.5230464e-14
2e-09	0.9996954	0.017449748	0	-6.091727e-14
3e-09	0.9993148	0.02616798	0	-1.3704653e-13

```

and in the headers there is a space between the mx and parenthesis, mx (), so I didn’t know what delimiter to use there, del = " " doesn’t work. Actually, I haven’t spent much time in DataFrames since Pandas works perfectly in this situation.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [November 27, 2021, 7:02pm UTC](https://discourse.julialang.org/t/pandas-dataframe-convert-to-array/72178/4 "2021-11-27T19:02:37Z")

</div>

This hack should let you automatically convert to Array without type piracy, but @nilshg 's answer may be the most idiomatic solution.

```julia
using Pandas
using Plots: Plots
plot(args...; kw...) = Plots.plot(Array.(args)...; kw...)
plot!(args...; kw...) = Plots.plot!(Array.(args)...; kw...)

df = read_table("M.out/table.txt")

x = df["# t (s)"]

plot(x, df["mx ()"])
plot!(x, df["my ()"])
plot!(x, df["mz ()"])

plot(1:10, sin.(1:10), title="normal usage still works")

```

---

<div class="post-metadata">

**Author:** ![libensvivit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/libensvivit/32/19006_2.png) [@libensvivit](https://discourse.julialang.org/u/libensvivit)\
**Post date:** [November 27, 2021, 7:23pm UTC](https://discourse.julialang.org/t/pandas-dataframe-convert-to-array/72178/5 "2021-11-27T19:23:52Z")

</div>

Thanks @Lilith! That’s what I was looking for!!

But anyways I found a more neat way by using DataFrames as @nilshg suggested:

```julia
using DataFrames, CSV
using Plots

df = DataFrame(CSV.File("makale.out/table.txt"))

x = df[!, "# t (s)"]

plot(x, df[!, "mx ()"])
plot!(x, df[!, "my ()"])
plot!(x, df[!, "mz ()"])

```

Thank you both for your quick reply!
