Python Forum
Retrieving a column from a data set using a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrieving a column from a data set using a function
#1
Hello all, this is my first time here so apologies if something is missed or in the wrong place.

I have recently started to learn to code and I am struggling with writing a function that will allow me to get all the data from a column in a data set by calling a function with inputs of the data set and a column number. I am completely stuck and any help would be appreciated.

My function starts like this:

def get_column(data, col_num): 
I already have the data so that does not need to be imported and col_num is the input to which I would type, for example:

print(get_column(data, 5))
and it would return the 4th column.

Thanks.
Reply
#2
What is data? You call it a data set, but I am not familiar with that data type. Is it a Pandas DataFrame? Provide more information about your data please. If possible provide the code that creates data
Reply
#3
(Oct-06-2021, 06:08 PM)deanhystad Wrote: What is data? You call it a data set, but I am not familiar with that data type. Is it a Pandas DataFrame? Provide more information about your data please. If possible provide the code that creates data

Sorry, the data is a .csv file. I am not currently using Pandas but I am using Numpy
Reply
#4
Could you please provide code that shows how you are reading the csv file.

if data is a 2D numpy array, getting column 2 is as simple as: column=data[:, 2]:
import numpy as np

data = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

# Print the columns one at a time.
for i in range(len(data[0])):
    print(i, data[:, i])
Output:
0 [1 5 9] 1 [ 2 6 10] 2 [ 3 7 11] 3 [ 4 8 12]
Reply
#5
I am reading the file such that

data = np.loadtxt('data.csv', delimiter = ',', skiprows = 1)
I need my function to get that column of data when I call the function,

e.g when I do print(get_column(data, 3)) it should call the function and get me the data from column 4 only
Reply
#6
If your data.csv file looks like mine, you can get columns the same way. No need to write a function.
import numpy as np

# data.csv looks like this: "these, are, column, headers\n1, 2, 3, 4\n5, 6, 7, 8\n9, 10, 11, 12"
data = np.loadtxt('data.csv', delimiter = ',', skiprows = 1)
print(f'np Array\n{data}\n\nColumns')

# Print the columns one at a time.
for i in range(len(data[0])):
    print(i, data[:, i])  # You don't need a function.  This does all you need
Output:
np Array [[ 1. 2. 3. 4.] [ 5. 6. 7. 8.] [ 9. 10. 11. 12.]] Columns 0 [1. 5. 9.] 1 [ 2. 6. 10.] 2 [ 3. 7. 11.] 3 [ 4. 8. 12.]
But if you need to write a function.
def getColumn(data, column):
    return data[:, column]
Reply
#7
Thank you so much for the help! It works perfectly now!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  KeyError while retrieving ESPN data john317ab 2 807 Nov-29-2023, 09:07 PM
Last Post: john317ab
  Returning Column and Row Data From Spreadsheet knight2000 0 435 Oct-22-2023, 07:07 AM
Last Post: knight2000
  .get() not retrieving value? Sedos101 2 560 Aug-25-2023, 11:48 AM
Last Post: deanhystad
  Database that can compress a column, or all data, automatically? Calab 3 1,164 May-22-2023, 03:25 AM
Last Post: Calab
  Code for pullng all data in a column EmBeck87 5 1,105 Apr-03-2023, 03:43 PM
Last Post: deanhystad
  [Solved] Retrieving a pdf from sqlite3 BigMan 4 2,313 Mar-12-2022, 01:56 PM
Last Post: deanhystad
  split txt file data on the first column value shantanu97 2 2,432 Dec-29-2021, 05:03 PM
Last Post: DeaD_EyE
  Python Pandas: How do I extract all the >1000 data from a certain column? JaneTan 0 1,556 Jul-17-2021, 09:09 AM
Last Post: JaneTan
  How to filter out Column data From Multiple rows data? firaki12345 10 5,094 Feb-06-2021, 04:54 AM
Last Post: buran
  Data extraction from a table based on column and row names tgottsc1 1 2,402 Jan-09-2021, 10:04 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020