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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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']).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0