【问题标题】:In pandas DataFrame, how to add column showing random selection result?在 pandas DataFrame 中,如何添加显示随机选择结果的列?
【发布时间】:2018-07-15 21:05:35
【问题描述】:

我到处都看到了如何在 pandas 中随机选择 DataFrame 行(有和没有 numpy)。我没有找到的是如何向 DataFrame 添加一列,以指示是否随机选择了一行。具体来说,我需要

1) 按 A 列中的值对行进行分组

2) 每组随机选取10行不放回

3) 添加一列 B 以指示是否选择了每一行 (TRUE/FALSE)。

结果应该是原始 DataFrame(即未分组),每行添加 TRUE/FALSE 列(这意味着,在其组内,该行是在随机选择期间选择的)。

我正在使用 python 3.6.2、pandas 0.20.3、numpy 1.13.1。

编辑以响应 cmets:

对于这个小数据样本,我们现在假设每个 ImageType 分组随机选择 2 行而不进行替换。是的,数据样本不是每个 ImageType 至少有 2 个。请理解,小数据集是为了防止发太长的帖子。

数据看起来像这样(有数千行):

+-----------+---------------------+
| ImageType |      FileName       |
+-----------+---------------------+
|         9 | PIC_001_01_0_9.JPG  |
|         9 | PIC_022_17_0_9.JPG  |
|        38 | PIC_100_00_0_38.jpg |
|         9 | PIC_293_12_0_9.JPG  |
|         9 | PIC_381_14_0_9.JPG  |
|        33 | PIC_001_17_2_33.JPG |
|         9 | PIC_012_07_0_9.JPG  |
|        28 | PIC_306_00_0_28.jpg |
|        28 | PIC_178_08_0_28.JPG |
|        26 | PIC_225_11_0_26.JPG |
|        18 | PIC_087_16_0_18.JPG |
|         9 | PIC_089_18_0_9.JPG  |
|        19 | PIC_090_18_0_19.JPG |
|         9 | PIC_091_18_0_9.JPG  |
|        19 | PIC_092_18_2_19.JPG |
|        23 | PIC_270_14_0_23.JPG |
|        13 | PIC_271_14_0_13.JPG |
+-----------+---------------------+

代码只是从 .csv 读取,但要重新创建上面的示例数据:

import pandas as pd
df = pd.DataFrame({'ImageType': ['9','9','38','9','9','33','9','28','28','26',
                                 '18','9','19','9','19','23','13'],
                   'FileName': ['PIC_001_01_0_9.JPG','PIC_022_17_0_9.JPG',
                                'PIC_100_00_0_38.jpg','PIC_293_12_0_9.JPG',
                                'PIC_381_14_0_9.JPG','PIC_001_17_2_33.JPG',
                                'PIC_012_07_0_9.JPG','PIC_306_00_0_28.jpg',
                                'PIC_178_08_0_28.JPG','PIC_225_11_0_26.JPG',
                                'PIC_087_16_0_18.JPG','PIC_089_18_0_9.JPG',
                                'PIC_090_18_0_19.JPG','PIC_091_18_0_9.JPG',
                                'PIC_092_18_2_19.JPG','PIC_270_14_0_23.JPG',
                                'PIC_271_14_0_13.JPG']})
# group by ImageType
# select 2 rows randomly in each group, without replacement
# add a column to original DataFrame to indicate selected rows

【问题讨论】:

  • 你能创建一些示例数据和预期的输出吗?
  • 将尽快提供。现在,让我说每个列/系列 dtype 都是对象(在这种情况下为文本),用于分组的列 A 大约有 10 个不同的值。
  • 您所要求的可能并不太难,但是如果人们有一些代码可以剪切和粘贴来重现您的情况,那么他们回答您的问题会容易得多。更多:minimal reproducible exampleHow to make good reproducible pandas examples
  • @pault 感谢您的建议。添加了示例数据和代码。
  • 您可能还会发现this post 很有帮助。

标签: python pandas numpy


【解决方案1】:
def get_sample(df, n=2):
    if len(df) <= n:
        df['Sampled'] = True
    else:
        s = df.sample(n=n)
        df['Sampled'] = df.apply(lambda x: x.name in s.index, axis=1)
    return df

grouped = df.groupby('ImageType')
new_df = grouped.apply(get_sample)

print(new_df)

               FileName ImageType  Sampled
0    PIC_001_01_0_9.JPG         9    False
1    PIC_022_17_0_9.JPG         9    False
2   PIC_100_00_0_38.jpg        38     True
3    PIC_293_12_0_9.JPG         9     True
4    PIC_381_14_0_9.JPG         9    False
5   PIC_001_17_2_33.JPG        33     True
6    PIC_012_07_0_9.JPG         9    False
7   PIC_306_00_0_28.jpg        28     True
8   PIC_178_08_0_28.JPG        28     True
9   PIC_225_11_0_26.JPG        26     True
10  PIC_087_16_0_18.JPG        18     True
11   PIC_089_18_0_9.JPG         9     True
12  PIC_090_18_0_19.JPG        19     True
13   PIC_091_18_0_9.JPG         9    False
14  PIC_092_18_2_19.JPG        19     True
15  PIC_270_14_0_23.JPG        23     True
16  PIC_271_14_0_13.JPG        13     True

如果组中的选项数量少于样本数量,它将对所有选项进行抽样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2017-12-26
    • 2019-08-19
    • 2021-01-04
    • 2021-09-21
    • 2019-10-22
    相关资源
    最近更新 更多