简体   繁体   English

iloc用pandas float64 vs int64设置值

[英]iloc to set values with pandas float64 vs int64

I am basically trying to set a value in a DataFrame using iloc with an index and a column name. 我基本上是试图在iFrame中使用带有索引和列名的iloc设置值。 Here is the test code: 这是测试代码:

import pandas as pd

df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
print type(df['a'][0])      # numpy.int64
df.iloc[-1]['a'] = 2000
print df                    # value changed to 2000
df['c'] = [3.5, 4.5]
print type(df['a'][0])      # numpy.float64 -> why does this change automatically?
print type(df['c'][0])      # numpy.float64
df.iloc[-1]['c'] = 2000     # yields warning, no value change
print df
df.iloc[-1]['a'] = 4000     # yields warning, no value change
print df

With Int64, I can do it, but not with Float64. 使用Int64,我可以做到,但不能使用Float64。 Is there an alternative? 还有其他选择吗? Or is this a bug? 还是这是一个错误? Thanks 谢谢

Jeff's answer is correct, but not optimal pandas syntax. 杰夫的答案是正确的,但不是最佳的熊猫语法。 It is considerably faster to to use .at instead of .loc (and likewise .iat instead of .iloc). 使用.at代替.loc(同样用.iat代替.iloc)要快得多。 If you're using this to get or set a lot of values, the time savings can really add up. 如果您使用它来获取或设置很多值,那么节省的时间就会增加。 The catch is that .at and .iat are meant only for getting and setting individual values, so you can't get or set a range of values or retrieve an entire column or row. 要注意的是,.at和.iat仅用于获取和设置单个值,因此您无法获取或设置值的范围或检索整个列或行。 Since all you want to do is change one value I would recommend: 由于您要做的只是更改一个值,因此我建议:

df.at[df.index[-1], 'a'] = 4000

If for whatever reason you only have the label-based location for one axis and the integer-based location on the other, you can also use df.get_loc('Label-based name of column') to get the integer-based location of that column or likewise df.index.get_loc('Label-based name of row') to get the integer-based location of that row. 如果出于某种原因,您仅在一个轴上具有基于标签的位置,而在另一轴上仅具有基于整数的位置,则还可以使用df.get_loc('基于标签的列名称')来获取该列或类似的df.index.get_loc('基于标签的行名称')以获取该行的基于整数的位置。

The warning is telling you that what you are doing is unsafe and it is because you have a mixed-type frame. 警告告诉您正在执行的操作是不安全的,这是因为您使用的是混合框架。 Instead use loc for this. 而是使用loc来实现。 See the docs on why this ia bad idea and may not work (which it doesn't here), http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 请参阅有关为什么这是一个坏主意并且可能无法正常工作的文档(此处无效), http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In [12]: df.loc[df.index[-1],'a'] = 4000

In [13]: df
Out[13]: 
      a  b    c
0     1  3  3.5
1  4000  4  4.5

[2 rows x 3 columns]

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

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