0

I am trying to read 5 columns from a 6 column csv data and use each row in a formula and itarete for all the rows.

import pandas as pd
df = pd.read_csv("/home/user/trial/blabla.csv", names=["name", "loc1", "loc2", "loc3", "loc4", "trial", "safe"])

a = something(b=df["trial"])
b1 = anotherhing(df["loc1"], df["loc2"], a)
b2 = anotherhing(df["loc3"], df["loc4"], a)

the file is csv file is something like this with hunderds of rows

john 12.1 22.3 33.5 88.2 0.2 1
jack 25.3 11.8 93.1 08.1 0.01 1

when I put values by hand it works all in well. However, when I put the df columns to do it for each row. It does not work. It reads all columns at once instead of 1 by 1.

What am I doing wrong here?

1 Answer 1

1

I am not sure if I got your question right, but doing:

df['loc1'] returns the entire column as a pandas series, so you will not get individual values for the columns.

It is hard to give a good suggestion since I dont really know what your something(...) function does, but if requiring individual values is what you want, you can do something like:

something_values = []

for row in df.itertuples(): 
  something_values.append(something(row[your_column_name]))

Either way, it might be worth looking into the dataframe method .apply() that is optimised for such operations, or column wise calculations (e.g. df['colA'] * df['colb']).

Sign up to request clarification or add additional context in comments.

2 Comments

the something is for the SkyCoord code from astropy package. I need 5 different columns from a row read and put there so do I need to do the value 5 times?
You can do .loc[row_index][column_name] or .iloc[row_number][column_name]. Alternatively, if you want to select multiple rows by some condition, you can look into slicing the dataframe: df[df[col_name] == some_value] e.t.c I will suggest having a look at the docs for more on this issue pandas.pydata.org/docs/user_guide/indexing.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.