回答編集履歴
1
追記
test
CHANGED
@@ -1,5 +1,95 @@
|
|
1
|
+
いくつか方法があります。
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
```python
|
2
6
|
|
3
7
|
df.assign(index=range(len(df)))
|
4
8
|
|
9
|
+
# または
|
10
|
+
|
11
|
+
df.assign(idx=np.arange(len(df)))
|
12
|
+
|
13
|
+
# または
|
14
|
+
|
15
|
+
df.assign(idx=pd.RangeIndex(len(df)))
|
16
|
+
|
5
17
|
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
---
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
速度計測
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```python
|
30
|
+
|
31
|
+
import benchit
|
32
|
+
|
33
|
+
import numpy as np
|
34
|
+
|
35
|
+
import pandas as pd
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
def by_reset_reset(df):
|
42
|
+
|
43
|
+
return df.reset_index().reset_index().set_index('index')
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
def by_reset(df):
|
50
|
+
|
51
|
+
return df.assign(idx=df.reset_index().index)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
def by_range(df):
|
58
|
+
|
59
|
+
return df.assign(idx=range(len(df)))
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
def by_np_arange(df):
|
66
|
+
|
67
|
+
return df.assign(idx=np.arange(len(df)))
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
def by_rangeindex(df):
|
74
|
+
|
75
|
+
return df.assign(idx=pd.RangeIndex(len(df)))
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
df = pd._testing.makeDataFrame()
|
82
|
+
|
83
|
+
t = benchit.timings([by_reset_reset, by_reset, by_range, by_np_arange, by_rangeindex],
|
84
|
+
|
85
|
+
[df.sample(n, replace=True, random_state=0)
|
86
|
+
|
87
|
+
for n in 10**np.arange(6)])
|
88
|
+
|
89
|
+
t.plot(logx=True, logy=True, figsize=(10, 6))
|
90
|
+
|
91
|
+
```
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
![イメージ説明](767c18490381bbbdd1506e0e05dde503.png)
|