# Select multiple columns from pandas dataframe

**URL:** <https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243>\
**Category:** General Usage\
**Created:** [November 16, 2020, 8:03pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243 "2020-11-16T20:03:43Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadaa](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@nadaa](https://discourse.julialang.org/u/nadaa)\
**Post date:** [November 16, 2020, 8:03pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/1 "2020-11-16T20:03:43Z")

</div>

Hi,  
I imported pandas and read CSV, I can print the data. I want to select the first 3 columns, it is working in Python as `df.iloc[:,:3]` but didn’t work in Julia. I also tried df[[“col1”,“col2”,“col3”]].  
Any suggestion, I don’t want to add other libraries.

Thanks

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [November 16, 2020, 8:19pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/2 "2020-11-16T20:19:45Z")

</div>

According to the usage section here [https://github.com/JuliaPy/Pandas.jl](https://github.com/JuliaPy/Pandas.jl)  
`df.iloc` should be `iloc(df)`

---

<div class="post-metadata">

**Author:** ![nadaa](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@nadaa](https://discourse.julialang.org/u/nadaa)\
**Post date:** [November 16, 2020, 8:44pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/3 "2020-11-16T20:44:50Z")

</div>

Thanks, I am using pandas which is already installed in conda. The one you mentioned is a new library.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 16, 2020, 9:28pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/4 "2020-11-16T21:28:48Z")

</div>

Sorry, what package are you using? Just PyCall?

---

<div class="post-metadata">

**Author:** ![nadaa](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@nadaa](https://discourse.julialang.org/u/nadaa)\
**Post date:** [November 16, 2020, 9:35pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/5 "2020-11-16T21:35:49Z")

</div>

Yes, just PyCall.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 16, 2020, 9:44pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/6 "2020-11-16T21:44:23Z")

</div>

Maybe it will work if you index with 1:3 instead of :3 which is not a range in Julia but the symbol 3

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 16, 2020, 9:57pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/7 "2020-11-16T21:57:52Z")

</div>

Can you give a full MWE?

---

<div class="post-metadata">

**Author:** ![nadaa](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@nadaa](https://discourse.julialang.org/u/nadaa)\
**Post date:** [November 17, 2020, 9:19am UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/8 "2020-11-17T09:19:51Z")

</div>

```julia
using PyCall

pd = pyimport("pandas")
np = pyimport("numpy")

function f()
	df = pd.read_csv("f.csv")
	print(df.iloc[:,:3])
end

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 17, 2020, 2:38pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/9 "2020-11-17T14:38:56Z")

</div>

Can you also post the error message as well?

---

<div class="post-metadata">

**Author:** ![nadaa](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@nadaa](https://discourse.julialang.org/u/nadaa)\
**Post date:** [November 18, 2020, 4:02pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/10 "2020-11-18T16:02:12Z")

</div>

```julia
MethodError: no method matching getindex(::PyObject, ::Colon, ::Int64)
Closest candidates are:
  getindex(::PyObject, !Matched::Integer, ::Integer) at deprecated.jl:70
  getindex(::PyObject, !Matched::Integer...) at deprecated.jl:70
  getindex(::PyObject, !Matched::T) where T<:Union{AbstractString, Symbol} at C:\Users\naghanem\.julia\packages\PyCall\zqDXB\src\PyCall.jl:326
  ...

Stacktrace:
 [1] fun() at .\In[7]:12
 [2] top-level scope at In[8]:1
 [3] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 18, 2020, 4:52pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/11 "2020-11-18T16:52:00Z")

</div>

I don’t know enough about either pandas or pycall to help you with this, unfortunately. Hopefully someone with more domain knowledge can help. Note that you still have `:3` instead of `0:3`, which may be causing an issue but I think is unlikely to be the main problem.

---

<div class="post-metadata">

**Author:** ![nadaa](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@nadaa](https://discourse.julialang.org/u/nadaa)\
**Post date:** [November 18, 2020, 5:45pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/12 "2020-11-18T17:45:12Z")

</div>

Thank you. I can print the whole df or one column, but I don’t know how to slice data from multiple columns!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 18, 2020, 6:35pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/13 "2020-11-18T18:35:30Z")

</div>

```julia
ulia> df = pd.DataFrame(rand(8,5));

julia> get(df[:iloc], (0:py"len"(df)-1,0:2))
PyObject 0 1 2
0 0.957077 0.598744 0.226023
1 0.177671 0.993589 0.037180
2 0.719229 0.271832 0.527947
3 0.538052 0.648124 0.251723
4 0.943631 0.770860 0.725607
5 0.610345 0.917904 0.775536
6 0.527918 0.751472 0.198443
7 0.158496 0.349875 0.057426

```

---

<div class="post-metadata">

**Author:** ![nadaa](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@nadaa](https://discourse.julialang.org/u/nadaa)\
**Post date:** [November 18, 2020, 6:46pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/14 "2020-11-18T18:46:42Z")

</div>

Thanks a lot, @jling, works amazingly!.  
Can you please just add a short explanation?  
Why do I need to use get?  
I have different Pythonic libraries I want to use!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 18, 2020, 7:37pm UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/15 "2020-11-18T19:37:21Z")

</div>

basically it’s because various overload in python such that you need to go a bit low-level.

`iloc` under the hood isn’t a simple function call because it’s the `iloc` tied to a `PyObject` so you can’t get the `df.iloc` first (at that point the pandas magic won’t work). So you need to manually use `get(` to tell `PyCall` those two arguments need to go inside `df.iloc` in python, not in julia.

see also: [https://github.com/JuliaPy/PyCall.jl#pyobject](https://github.com/JuliaPy/PyCall.jl#pyobject)

---

<div class="post-metadata">

**Author:** ![nadaa](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@nadaa](https://discourse.julialang.org/u/nadaa)\
**Post date:** [November 19, 2020, 11:30am UTC](https://discourse.julialang.org/t/select-multiple-columns-from-pandas-dataframe/50243/16 "2020-11-19T11:30:56Z")

</div>

Thank you @jling!
