当前位置:主页 > 软件编程 > Python代码 >

pandas.dataframe按行索引表达式选取方法

时间:2021-09-16 09:55:15 | 栏目:Python代码 | 点击:

需要把一个从csv文件里读取来的数据集等距抽样分割,这里用到了列表表达式和dataframe.iloc

先生成索引列表:

index_list = ['%d' %i for i in range(df.shape[0]) if i % 3 == 0]

在dataframe中选取

sample_df = df.iloc[index_list]

合起来

sample_df = df.iloc[['%d' %i for i in range(df.shape[0]) if i % 3 == 0]]

各位大神有没有更好的办法?望不吝赐教。

您可能感兴趣的文章:

相关文章