1
    raw=pd.read_csv('raw_6_12_8_30.csv')
    raw2=raw.loc[raw['spices'].isnull()==False]  # code for deleting 10 values #

    b=[]

    for i in range(len(raw2)):
        if raw2['Status'][i]==0:            # codes didn't run perfectly#
            print(i)

But when I use this code without line 2, it works fine.

    raw=pd.read_csv('raw_6_12_8_30.csv')
    b=[]

    for i in range(len(raw)):
        if raw['Status'][i]==0:            
            print(i)

I checked there is no errors in this raw2['Status] and raw['Status']

But whenever I use pandas.loc ,there is an error.

I bet that line 2 makes an error but I don't know why?

error images here enter image description here

key errors 11 # what is it #

1
  • 1
    In your own words: when you do for i in range(len(raw)), what values will i have? Now, you are using the values like so: raw['Status'][i]. Does that make sense? How many things are in raw? How many things are in raw['Status']? Do you expect those answers to be the same? Why? Commented Jun 13, 2020 at 11:13

3 Answers 3

1

there are 3 ways to get values from dataframe by indexing.

  1. loc gets rows (or columns) with particular labels from the index.

  2. iloc gets rows (or columns) at particular positions in the index (so it only takes integers).

  3. ix usually tries to behave like loc but falls back to behaving like iloc if a label is not present in the index.

if you want to take values by indexing you can use iloc. Like in the code below

raw=pd.read_csv('raw_6_12_8_30.csv')
b=[]

for i in range(len(raw)):
    if raw['Status'].iloc[i]==0:            
        print(i)
Sign up to request clarification or add additional context in comments.

Comments

0

can you try with :

for i in range(0,len(raw)-1):

i guess key error 11 occur because of index range. the key 11 may be out of range.

2 Comments

no... it doesn't work. to solve the question i have to make new csv file like raw2.to_csv('raw3.csv) and i recall the new table raw3= pd.read_csv('raw3.csv') and then it works. i don't know why
hmm, interesting. can you share the exact error message?
0

Are you trying to drop all rows where spices is null?

raw.dropna(subset="spices", inplace=True)

To print where the status is 0:

raw_subset = raw[raw["Status"]==0]
print(raw_subset)

# To get the specific indices
print(raw_subset.index)

Comments

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.