Pandas 数据框:按列和行替换值的性能是不是有差异?

Posted

技术标签:

【中文标题】Pandas 数据框:按列和行替换值的性能是不是有差异?【英文标题】:Pandas dataframe: Is there a difference in performance in replacing values by column and row?Pandas 数据框:按列和行替换值的性能是否有差异? 【发布时间】:2016-11-02 07:21:43 【问题描述】:

我有以下熊猫数据框:

import pandas as pd
df = pd.read_csv('filename.csv')

print(df)

     dog      A         B           C
0     dog1    0.787575  0.159330    0.053095
1     dog10   0.770698  0.169487    0.059815
2     dog11   0.792689  0.152043    0.055268
3     dog12   0.785066  0.160361    0.054573
4     dog13   0.795455  0.150464    0.054081
5     dog14   0.794873  0.150700    0.054426
..    ....
8     dog19   0.811585  0.140207    0.048208
9     dog2    0.797202  0.152033    0.050765
10    dog20   0.801607  0.145137    0.053256
11    dog21   0.792689  0.152043    0.055268
    ....

我知道0 行和df['dog'] 列中的第一个值需要显式更改。而不是dog1,它应该是dog11

用户可以通过哪些方式更改此值?它们之间的性能是否存在实质性差异?

【问题讨论】:

【参考方案1】:

如果要按列名选择,可以使用ixloc

df.ix[0, 'dog'] = 'dog11'
df.loc[0, 'dog'] = 'dog11'

最快的是 iat,如果您想按位置选择:dog 位置(例如,在此示例的第一列 - 0):

df.iat[0, 0] = 'dog11'

时间安排

In [144]: %%timeit
     ...: df.ix[0, 'dog'] = 'dog11'
The slowest run took 5.37 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 308 µs per loop

In [146]: %%timeit
     ...: df.loc[0, 'dog'] = 'dog11'
     ...: 
The slowest run took 5.03 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 305 µs per loop

In [145]: %%timeit
     ...: df.iat[0, 0] = 'dog11'
The slowest run took 53.64 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 8.18 µs per loop

In [151]: %%timeit
     ...: df.iloc[0, 0] = 'dog11'
     ...: 
The slowest run took 5.69 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 392 µs per loop

【讨论】:

Fastest is iat, but column dog has to be first 为什么这么说?

以上是关于Pandas 数据框:按列和行替换值的性能是不是有差异?的主要内容,如果未能解决你的问题,请参考以下文章

列和行操作 Python Pandas

删除熊猫数据框中包含特定值的列和行[重复]

Pandas列表的列,通过迭代(选择)三列的每个列表元素作为新列和行来创建多列[重复]

用 Pandas 将 DataFrame 中某些列和行的值替换为同一 DataFrame 中另一列的值

每天按列和行、计数和百分比分组

迭代 Pandas Dataframe 中的列和行