一、总结
一句话总结:
pandas可以用sample方法返回random sample,可以用reset_index方法reset打乱之后的index
df=df.sample(frac=1.0) #打乱所有数据
df=df.reset_index(drop=True) #打乱后的数据index也是乱的,用reset_index重新加一列index,drop=True表示丢弃原有index一列
二、pandas打乱数据集
import pandas as pd
一、sample方法随机打乱数据集
In [6]:
data = pd.read_csv('./iris.data',header=None)
data
Out[6]:
| 0 | 1 | 2 | 3 | 4 |
0 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa |
... | ... | ... | ... | ... | ... |
145 | 6.7 | 3.0 | 5.2 | 2.3 | Iris-virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | Iris-virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | Iris-virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | Iris-virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | Iris-virginica |
150 rows × 5 columns
In [7]:
#设置frac=0.5表示随机抽取50%的数据
data=data.sample(frac=1.0)#打乱所有数据
data
Out[7]:
| 0 | 1 | 2 | 3 | 4 |
36 | 5.5 | 3.5 | 1.3 | 0.2 | Iris-setosa |
43 | 5.0 | 3.5 | 1.6 | 0.6 | Iris-setosa |
93 | 5.0 | 2.3 | 3.3 | 1.0 | Iris-versicolor |
117 | 7.7 | 3.8 | 6.7 | 2.2 | Iris-virginica |
70 | 5.9 | 3.2 | 4.8 | 1.8 | Iris-versicolor |
... | ... | ... | ... | ... | ... |
66 | 5.6 | 3.0 | 4.5 | 1.5 | Iris-versicolor |
12 | 4.8 | 3.0 | 1.4 | 0.1 | Iris-setosa |
101 | 5.8 | 2.7 | 5.1 | 1.9 | Iris-virginica |
81 | 5.5 | 2.4 | 3.7 | 1.0 | Iris-versicolor |
2 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa |
150 rows × 5 columns
为了结果的复现
可以看到设置的种子一样的时候,生成的随机数是一样的
In [9]:
data = pd.read_csv('./iris.data',header=None)
data=data.sample(frac=1.0,random_state=11)#打乱所有数据
data
Out[9]:
| 0 | 1 | 2 | 3 | 4 |
112 | 6.8 | 3.0 | 5.5 | 2.1 | Iris-virginica |
145 | 6.7 | 3.0 | 5.2 | 2.3 | Iris-virginica |
133 | 6.3 | 2.8 | 5.1 | 1.5 | Iris-virginica |
56 | 6.3 | 3.3 | 4.7 | 1.6 | Iris-versicolor |
111 | 6.4 | 2.7 | 5.3 | 1.9 | Iris-virginica |
... | ... | ... | ... | ... | ... |
76 | 6.8 | 2.8 | 4.8 | 1.4 | Iris-versicolor |
13 | 4.3 | 3.0 | 1.1 | 0.1 | Iris-setosa |
81 | 5.5 | 2.4 | 3.7 | 1.0 | Iris-versicolor |
91 | 6.1 | 3.0 | 4.6 | 1.4 | Iris-versicolor |
80 | 5.5 | 2.4 | 3.8 | 1.1 | Iris-versicolor |
150 rows × 5 columns
二、reset_index方法可以重新设置index(打乱数据集之后)
In [10]:
data = pd.read_csv('./iris.data',header=None)
data
Out[10]:
| 0 | 1 | 2 | 3 | 4 |
0 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa |
... | ... | ... | ... | ... | ... |
145 | 6.7 | 3.0 | 5.2 | 2.3 | Iris-virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | Iris-virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | Iris-virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | Iris-virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | Iris-virginica |
150 rows × 5 columns
In [11]:
data=data.sample(frac=1.0)#打乱所有数据
data
Out[11]:
| 0 | 1 | 2 | 3 | 4 |
69 | 5.6 | 2.5 | 3.9 | 1.1 | Iris-versicolor |
91 | 6.1 | 3.0 | 4.6 | 1.4 | Iris-versicolor |
20 | 5.4 | 3.4 | 1.7 | 0.2 | Iris-setosa |
19 | 5.1 | 3.8 | 1.5 | 0.3 | Iris-setosa |
114 | 5.8 | 2.8 | 5.1 | 2.4 | Iris-virginica |
... | ... | ... | ... | ... | ... |
82 | 5.8 | 2.7 | 3.9 | 1.2 | Iris-versicolor |
94 | 5.6 | 2.7 | 4.2 | 1.3 | Iris-versicolor |
73 | 6.1 | 2.8 | 4.7 | 1.2 | Iris-versicolor |
85 | 6.0 | 3.4 | 4.5 | 1.6 | Iris-versicolor |
65 | 6.7 | 3.1 | 4.4 | 1.4 | Iris-versicolor |
150 rows × 5 columns
In [12]:
data=data.reset_index(drop=True) #打乱后的数据index也是乱的,用reset_index重新加一列index,drop=True表示丢弃原有index一列
data
Out[12]:
| 0 | 1 | 2 | 3 | 4 |
0 | 5.6 | 2.5 | 3.9 | 1.1 | Iris-versicolor |
1 | 6.1 | 3.0 | 4.6 | 1.4 | Iris-versicolor |
2 | 5.4 | 3.4 | 1.7 | 0.2 | Iris-setosa |
3 | 5.1 | 3.8 | 1.5 | 0.3 | Iris-setosa |
4 | 5.8 | 2.8 | 5.1 | 2.4 | Iris-virginica |
... | ... | ... | ... | ... | ... |
145 | 5.8 | 2.7 | 3.9 | 1.2 | Iris-versicolor |
146 | 5.6 | 2.7 | 4.2 | 1.3 | Iris-versicolor |
147 | 6.1 | 2.8 | 4.7 | 1.2 | Iris-versicolor |
148 | 6.0 | 3.4 | 4.5 | 1.6 | Iris-versicolor |
149 | 6.7 | 3.1 | 4.4 | 1.4 | Iris-versicolor |
150 rows × 5 columns
In [ ]: