我有以下pandas DataFrame:
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
。
用户可以通过哪些方式更改此值?他们之间的表现有很大差异吗?
答案 0 :(得分:1)
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