简体   繁体   English

编辑熊猫数据框中的特定单元格?

[英]Editing specific cells in pandas dataframe?

I'm trying to write a function that will enable a user to easily select a specific cell in a pandas dataframe and change its value. 我正在尝试编写一个函数,使用户可以轻松地在pandas数据框中选择特定的单元格并更改其值。 For instance, in the dataframe below, I would like to have a user be able to select a cell by date and action, say "2018-03-02" and "Manure", and change the value to "1". 例如,在下面的数据框中,我希望用户能够按日期和操作选择一个单元格,例如“ 2018-03-02”和“ Manure”,并将值更改为“ 1”。

Sample Chart 样本图

I have done this already at 2018-03-02 and "Cutting" by using this code (the dataframe is named q): 我已经在2018-03-02和使用以下代码“剪切”完成了此操作(数据框名为q):

q.iat[1,1]=1

However, I would like to be able to change the cell using something like q.iat["2018-03-02", "Cutting"]=1 , but am not sure how to do this, as it says .iat only recognizes integer postions. 但是,我希望能够使用类似q.iat["2018-03-02", "Cutting"]=1来更改单元格,但不确定如何执行此操作,因为它说.iat仅能识别整数位置。 Is there something besides .iat I could use, or would I need to set a dictionary for every date and every column? 我除了可以使用.iat以外,还需要为每个日期和每一列设置一个词典吗?

Your dataframe is indexed with columns names and index. 您的数据框已使用列名和索引建立索引。 You can set your index with set_index : df.set_index('date') 您可以使用set_index设置索引: df.set_index('date')
Then use both your date index and 'Cutting' column to change a value with .loc : df.loc('2018-03-02','Cutting') = 1 然后使用日期索引和'剪切'列更改.loc的值: df.loc('2018-03-02','Cutting') = 1

pd.DataFrame.at

Use at instead of iat for efficient scalar label-based indexing: 使用at而不是iat进行有效的基于标量标签的索引:

q.at['2018-03-02', 'Cutting'] = 1

To summarise : 总结一下

A short guide to indexing: 索引编制的简短指南:

  1. Use iat / at for scalar access / setting by integer position or label respectively. 使用iat / at分别通过整数位置或标签进行标量访问/设置。
  2. Use iloc / loc for non-scalar access / setting by integer position or label respectively. 使用iloc / loc分别通过整数位置或标签进行非标量访问/设置。

You can select columns by name in pandas simply using its name: 您可以使用熊猫的名称来按名称选择列:

q['Manure']

In order to select the rows you need an index to uniquely identify them, that could be the date if you make sure its in a format that uniquely identifies it. 为了选择行,您需要一个索引来唯一地标识它们,如果您以唯一标识它的格式来确定日期,则可以是日期。 Eg: 例如:

q = q.set_index(pd.DatetimeIndex(q['Dates']))

Then you can select each cell doing: 然后,您可以选择执行以下操作的每个单元:

q.loc[<rowIndex>, <colName>]

You can use DataFrame.loc, this should get you started: https://pandas.pydata.org/pandas-docs/stable/indexing.html 您可以使用DataFrame.loc,这应该可以帮助您入门: https ://pandas.pydata.org/pandas-docs/stable/indexing.html

In your example: 在您的示例中:

q.loc["2018-03-02","Manure"]=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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