0

I am trying to call a function from a different module as below:

module1 - func1: returns a dataframe

module1 - func2(p_df_in_fromfunc1)

function 2:

for i in range(0,len(p_df_in_fromfunc1):
    # Trying to retrieve row values of individual columns and assign to variables
    v_tmp = p_df_in_fromfunc1.loc[i,"Col1"]

When trying to run the above code, I get the error:

KeyError 0

Could the issue be because I don't have a zero numbered row?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Suraj Sareen
  • 71
  • 1
  • 7
  • I edited your post, including your code snippet, whch was not valid python. Please check it – leoschet Feb 10 '20 at 18:33
  • Does this answer your question? [How to select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – leoschet Feb 10 '20 at 18:36
  • I assume that is pretty much due to your index of dataframe. Is your data indexed with raw incremental row numbers? If so, did you do any filtering on data (func1 perhaps) so that the ith location is not present in the df. – null Feb 10 '20 at 18:41
  • @null - Yes, I found the problem. My index does not have zero, since I had created a subset of my original dataframe, which is what is used now to do my calculations. How can I resolve the issue? – Suraj Sareen Feb 10 '20 at 18:52
  • I found the issue and fixed it (reset the index) per null's suggestion. Is their a better solution, when we have to take a subset of a dataframe and pass it to a function for performing calculations? – Suraj Sareen Feb 10 '20 at 19:01
  • try `df.reset_index(drop=True, inplace=True)` assuming your dateframe variable is `df`. – null Feb 11 '20 at 13:54

3 Answers3

1

Without knowing much of you're code, well my guess is, for positional indexing try using iloc instead of loc, if you're interesed in going index-wise. Something like:

v_tmp = p_df_in_fromfunc1.iloc[i,"Col1"]
Saurav Joshi
  • 414
  • 5
  • 13
0

For retrieving a row for specific columns do:

columns = ['Col1', 'Col2']
df[columns].iloc[index]

If you only want one column, you can simplify it to: df['Col1'].iloc(index)

As per your comment, you do not need to reset the index, you can iterate over the values of your index array: df.index

leoschet
  • 1,697
  • 17
  • 33
0

You may have a missed to close the quote in the loc function after Col1 ?

v_tmp = p_df_in_fromfunc1.loc[i,"Col1"]
Sajan
  • 1,247
  • 1
  • 5
  • 13