美文网首页
[python][科学计算][pandas]简要使用教程7-乱序

[python][科学计算][pandas]简要使用教程7-乱序

作者: jiedawang | 来源:发表于2019-03-18 13:19 被阅读0次

最后一次更新日期: 2019/3/17

pandas是基于numpy的数据分析库,提供一些更易用的数据模型和大量高效的统计方法。

使用前先导入模块:
import pandas as pd
按需导入以下模块:
import numpy as np
import matplotlib.pyplot as plt

1. 随机排序

In [24]: df=pd.DataFrame({'a':[1,2,3],'b':[4,5,6]})

In [25]: df.iloc[np.random.permutation(df.shape[0])]
Out[25]: 
   a  b
1  2  5
0  1  4
2  3  6

In [27]: df.sample(frac=1.)
Out[27]: 
   a  b
0  1  4
2  3  6
1  2  5

随机排序,用于打乱数据集。一种方法是通过numpy生成乱序索引,然后应用在pandas的iloc索引方法上;另一种方法是使用pandas的抽样方法sample,设置抽样比例frac参数为1.,采用默认的不放回抽样的方式,也可以达到同样的效果。

2. 抽样

In [34]: df.sample(n=2)
Out[34]: 
   a  b
1  2  5
2  3  6

In [36]: df.sample(frac=0.8,replace=True)
Out[36]: 
   a  b
1  2  5
1  2  5

sample方法用于抽样,第一个参数n设置抽样数量,第二个参数frac设置抽样比例,第三个参数replace设置是否放回,默认False,第四个参数weight可设置样本权重,第五个参数random_state设置随机数种子。

相关文章

网友评论

      本文标题:[python][科学计算][pandas]简要使用教程7-乱序

      本文链接:https://www.haomeiwen.com/subject/ldyvmqtx.html