pandas 数据框中的随机选择 [英] Random selection in pandas dataframe
本文介绍了 pandas 数据框中的随机选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解决这个更复杂的问题.这是一个较小的问题:
I'm trying to solve this more complicated question. Here's a smaller problem:
给出df
a b
1 2
5 0
5 9
3 6
1 8
如何创建列C,该列C是同一行df ['a']和df ['b']两个元素之间的随机选择?
How can I create a column C that is a random selection between the two elements of df['a'] and df['b'] of the same row?
因此,给定此伪df,随机运算符将从行#1的(1,2)对中选择行#2的(5,0)对...等等.
So, given this dummy df, the random operator would choose from the pair (1, 2) for row #1, from (5, 0) for row #2...etc.
谢谢
推荐答案
import random
n = 2 # target row number
random.sample(df.iloc[n, :2], 1) # Pick one number from this row.
对于整个数据框:
>>> df.loc[:, ['a', 'b']].apply(random.sample, args=(1,), axis=1)
0 [1]
1 [5]
2 [9]
3 [3]
4 [8]
dtype: object
清理它以提取单个值:
>>> pd.Series([i[0] for i in df.loc[:, ['a', 'b']].apply(random.sample, args=(1,), axis=1)], index=df.index)
0 1
1 5
2 9
3 3
4 8
dtype: int64
或者利用列'a'的索引为零(False)和列'b'的索引为1(True):
Or taking advantage that column 'a' is indexed at zero (False) and column 'b' is indexed at 1 (True):
>>> [df.iat[i, j] for i, j in enumerate(1 * (np.random.rand(len(df)) < .5))]
[1, 5, 5, 6, 8]
这篇关于 pandas 数据框中的随机选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文