[英]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.我正在努力更改 pandas 内一行内的值。
I will include the first two lines of my.csv file, so you can get a feel for the data.我将包含 my.csv 文件的前两行,以便您对数据有所了解。
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?注意:由于某种原因,我无法通过 localList.iloc['seats'] 访问数据,但也许我做错了?
I am unable to figure out how to get the seats to decrement by 1 each time one is purchased.我无法弄清楚每次购买一个座位时如何让座位减少 1。 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.
我尝试将值设置为等于自身减去 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.
SettingWithCopyWarning:试图在 DataFrame 的切片副本上设置值。 Try using.loc[row_indexer,col_indexer] = value instead
尝试改用 .loc[row_indexer,col_indexer] = value
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
请参阅文档中的警告: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self.obj[item] = s
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.在我看到之后,我多次按下购买按钮,但它总是停留在之前的数据 - 1,并且永远不会进一步减少。 So I tried what was given to me in the console.
所以我尝试了控制台中给我的东西。 Using LOC instead of ILOC
使用 LOC 代替 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'
TypeError: 无法使用 class 'int' 的这些索引器 [2] 对 label 进行索引
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.然后,我想将其限制为一个变量,以测试我是否甚至可以使用 LOC 触摸这些数据,这似乎是我做不到的。
localList.loc[0, 2] -= 1
TypeError: cannot do label indexing on with these indexers [2] of class 'int'
TypeError: 无法使用 class 'int' 的这些索引器 [2] 对 label 进行索引
From here I wanted to see what I was working with using LOC instead of ILOC.从这里我想看看我使用 LOC 而不是 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?
它与 ILOC 没有什么不同,那么为什么我不能以同样的方式访问这些数据呢?
print(localList.loc[0])
section 101
第 101 条
price 50
价格 50
seats 150
座位 150
type matinee
类型 matinee
Name: 0, dtype: object
名称:0,数据类型:object
So I didnt think that saving off the slice would stop it from updating the dataframe.所以我不认为保存切片会阻止它更新 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.
因此,在测试时,我发现我需要获取我的 localList 并将其保存回最初选择它的框架中。
Edit : I understood the question now.编辑:我现在明白了这个问题。 You are trying to update the original dataframe
matineeSeats.df
and not localList
您正在尝试更新原始 dataframe
matineeSeats.df
而不是localList
You are creating a copy with the .loc
selection您正在使用
.loc
选择创建副本
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
To update matineeSeats_df
directly you can do it like this:要直接更新
matineeSeats_df
,您可以这样做:
matineeSeats_df.loc[matineeSeats_df['section'] == 101, 'seats'] -= 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.