Pandas dataframe:用列和行替换值在性能上有区别吗?

2024-05-16 21:32:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧:

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']列,第一个值需要显式更改。它应该是dog11,而不是dog1。你知道吗

用户可以通过哪些方式更改此值?他们之间的表现有实质性的区别吗?你知道吗


Tags: csv数据importpandasdfreadasfilename
1条回答
网友
1楼 · 发布于 2024-05-16 21:32:13

如果要按列名选择,可以使用^{}^{}

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

最快的是^{},如果您想按位置选择: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

相关问题 更多 >