简体   繁体   English

在 python 中使用 Pandas,如何更改某行?

[英]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'] 访问数据,但也许我做错了?


What I tried我试过的

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,并得到以下结果。

Try 1试试 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

Try 2试试 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.在我看到之后,我多次按下购买按钮,但它总是停留在之前的数据 - 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 进行索引

Try 3尝试 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.然后,我想将其限制为一个变量,以测试我是否甚至可以使用 LOC 触摸这些数据,这似乎是我做不到的。

localList.loc[0, 2] -= 1

TypeError: cannot do label indexing on with these indexers [2] of class 'int' TypeError: 无法使用 class 'int' 的这些索引器 [2] 对 label 进行索引

Try 4尝试 4

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

What fixed it for me是什么为我解决了它

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

Problem问题

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

Solution解决方案

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.

相关问题 如何使用python pandas删除每行中的某些单元格? - How to delete certain cells in each row, using python pandas? 如何在 python 中使用 Pandas 数据框按特定日期和小时进行过滤 - How do I filter by a certain date and hour using Pandas dataframe in python (Python,熊猫)-如何将所有内容保留在某个角色的左侧? - (Python, Pandas) - How do I get everything to the left of a certain character? 如何根据其他列中的数据替换python大熊猫中的某些值? - How do you replace certain values from row to row in python pandas based on data in other columns? 如何使用Pandas查找具有特定日期的所有行? - How do I find all rows with a certain date using Pandas? 使用Python如何在Pandas数据帧中的每一行的范围内生成一个随机数? - Using Python how do I generate a random number within a range for each row in Pandas dataframe? 我如何使用 python pandas 数据框并使用列名和行名作为新列创建一个新表 - how do i take a python pandas dataframe and create a new table using the column and row names as the new column 如何使用 Pandas 在 Python 中基于同一行中的另一个单元格设置单元格值 - How do I set a cell value based on another cell in same row in Python Using Pandas 如何以某种方式更改数据框pandas python? - How to change a dataframe a certain way pandas python? 如何使用 Z3A43B4F88325D94022C0EFA 库在 python 的 2 列 CSV 文件上更改 header 而不创建新的 C9 文件? - How do I change the header on a 2 column CSV file in python using the pandas library without creating a new file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM