简体   繁体   中英

Using Pandas in python, how do I change a certain row?

I am working on changing a value inside of a row inside of pandas.

I will include the first two lines of my.csv file, so you can get a feel for the data.

section,price,seats,type
101,50.00,150,matinee

As you can see, it is pretty straight forward. Here is the issue.

localList = matineeSeats.df.loc[matineeSeats.df['section'] == int(selection)] #Create slice of DataFrame for selected section
        if localList.iloc[0, 2] > 0: #If theres more than 0 seats left... Cant do [0, 'seats']
            print("Taking seat")
            #Set the seats -= 1 ###THIS LINE###

NOTE: For some reason i cannot access data by doing localList.iloc['seats'], but maybe i am doing it wrong?


What I tried

I am unable to figure out how to get the seats to decrement by 1 each time one is purchased. The "THIS LINE" is where all my problems come from. I tried just setting the value equal to itself minus 1, and got the following.

Try 1

            if localList.iloc[0, 2] > 0:
                 print("Taking seat")
                 localList.iloc[0, 2] = localList.iloc[0, 2] - 1
                 print(localList.iloc[0, 2])

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using.loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self.obj[item] = s

Try 2

After I saw that, I pressed the buy button multiple times but it ALWAYS stayed at the previous data - 1, and would never decrement further than that. So I tried what was given to me in the console. Using LOC instead of ILOC

            if localList.iloc[0, 2] > 0:
                 print("Taking seat")
                 localList.loc[0, 2] = localList.loc[0, 2] - 1
                 print(localList.iloc[0, 2])

TypeError: cannot do label indexing on with these indexers [2] of class 'int'

Try 3

I wanted to then just limit this to one variable to test if I can even touch this data with LOC, which it seems that I cant.

localList.loc[0, 2] -= 1

TypeError: cannot do label indexing on with these indexers [2] of class 'int'

Try 4

From here I wanted to see what I was working with using LOC instead of ILOC. So I just printed the data out. It's no different from ILOC, so why can I not access this data the same way?

 print(localList.loc[0])

section 101

price 50

seats 150

type matinee

Name: 0, dtype: object

What fixed it for me

So I didnt think that saving off the slice would stop it from updating the dataframe. So while testing I figure out I need to take my localList and save it back into the frame where it was selected in the first place.

Edit : I understood the question now. You are trying to update the original dataframe matineeSeats.df and not localList

Problem

You are creating a copy with the .loc selection

import pandas as pd
matineeSeats_df = pd.DataFrame([{'section': 101, 'price': 50.0, 'seats': 150, 'type': 'matinee'}])

# this creates a copy
localList = matineeSeats_df.loc[matineeSeats_df['section'] == 101]
# just localList gets updated, matineeSeats_df is not updated
localList.at[0, 'seats'] = localList.at[0, 'seats'] - 1

Solution

To update matineeSeats_df directly you can do it like this:

matineeSeats_df.loc[matineeSeats_df['section'] == 101, 'seats'] -= 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM