pandas庫疑難問題---1、pandas打亂數據集
一、總結
一句話總結:
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打亂數據集
博客對應課程的視頻位置:1、pandas打亂數據集-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/39/360
import pandas as pd
一、sample方法隨機打亂數據集
In [6]:
data = pd.read_csv('./iris.data',header=None) data
Out[6]:
In [7]:
#設置frac=0.5表示隨機抽取50%的數據
data=data.sample(frac=1.0)#打亂所有數據 data
Out[7]:
為了結果的復現
可以看到設置的種子一樣的時候,生成的隨機數是一樣的
In [9]:
data = pd.read_csv('./iris.data',header=None) data=data.sample(frac=1.0,random_state=11)#打亂所有數據 data
Out[9]:
二、reset_index方法可以重新設置index(打亂數據集之后)
In [10]:
data = pd.read_csv('./iris.data',header=None) data
Out[10]:
In [11]:
data=data.sample(frac=1.0)#打亂所有數據 data
Out[11]:
In [12]:
data=data.reset_index(drop=True) #打亂后的數據index也是亂的,用reset_index重新加一列index,drop=True表示丟棄原有index一列 data
Out[12]:
In [ ]:
博客對應系列課程視頻位置:
1、pandas打亂數據集-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/39/360
2、pandas切片操作-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/39/379
3、loc方法和iloc方法的區別-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/39/380
4、DataFrame類型轉換成Numpy中ndarray-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/39/381
1、pandas打亂數據集-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/39/360
2、pandas切片操作-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/39/379
3、loc方法和iloc方法的區別-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/39/380
4、DataFrame類型轉換成Numpy中ndarray-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/39/381