Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用熊猫中的随机数据对进行选择_Python_Pandas - Fatal编程技术网

Python 使用熊猫中的随机数据对进行选择

Python 使用熊猫中的随机数据对进行选择,python,pandas,Python,Pandas,我有一个非常大的文件,有三列。前两个是整数,第三个是字符串。我在《使用熊猫》一书中读到 data = pd.read_csv("edges+stuff.txt", sep=' ', header=None, dtype={0:np.uint32, 1:np.uint32, 2:np.str}) 以下是一些假数据示例: 2 0 Somestuff9 2 0 Somestuff0 1 1 Somestuff5 0 0 Somestuff7 2 0 Somestuff9 2 0 Somestuff5

我有一个非常大的文件,有三列。前两个是整数,第三个是字符串。我在《使用熊猫》一书中读到

data = pd.read_csv("edges+stuff.txt", sep=' ', header=None, dtype={0:np.uint32, 1:np.uint32, 2:np.str})
以下是一些假数据示例:

2 0 Somestuff9
2 0 Somestuff0
1 1 Somestuff5
0 0 Somestuff7
2 0 Somestuff9
2 0 Somestuff5
2 1 Somestuff2
1 1 Somestuff8
1 1 Somestuff2
1 0 Somestuff4
2 1 Somestuff3
0 2 Somestuff9
1 1 Somestuff10
1 0 Somestuff9
我想进行以下我一直坚持的随机抽样。我想从数据帧中选取一些随机对。我不想随机选取一行,例如“11”出现四次,但我希望有同等的机会选取数据帧中存在的任何一对。如果我选择了“11”,那么我想输出所有以“11”开头的行

使用我的示例假数据,我想从[(0,0),(1,0),(1,1),(0,2),(2,0),(2,1)](这些都是数据中存在的对)中随机选择一些对,然后使用这些对从数据帧中选择行

实现这一点的一种方法是获取前两列,对它们进行排序,并执行等效的
np.unique
。然后从该唯一列表中选择随机对,然后使用它们从原始数据帧中进行选择

有什么方法可以在熊猫身上有效地做到这一点吗

这里有一个方法:

df.head()
Out: 
   col1  col2        col3
0     2     0  Somestuff9
1     2     0  Somestuff0
2     1     1  Somestuff5
3     0     0  Somestuff7
4     2     0  Somestuff9
随机选择一对:

df[['col1', 'col2']].drop_duplicates().sample(n=1)
Out: 
   col1  col2
0     2     1
(此处删除重复项删除除第一个col1 col2对之外具有相同col1 col2对的所有行,并
.sample(n=1)
从中选择一个。)

具有
col1=2
col2=1
的所有行:

df[['col1', 'col2']].drop_duplicates().sample(n=1).merge(df)
Out: 
   col1  col2        col3
0     2     1  Somestuff2
1     2     1  Somestuff3
这里有一个方法:

df.head()
Out: 
   col1  col2        col3
0     2     0  Somestuff9
1     2     0  Somestuff0
2     1     1  Somestuff5
3     0     0  Somestuff7
4     2     0  Somestuff9
随机选择一对:

df[['col1', 'col2']].drop_duplicates().sample(n=1)
Out: 
   col1  col2
0     2     1
(此处删除重复项删除除第一个col1 col2对之外具有相同col1 col2对的所有行,并
.sample(n=1)
从中选择一个。)

具有
col1=2
col2=1
的所有行:

df[['col1', 'col2']].drop_duplicates().sample(n=1).merge(df)
Out: 
   col1  col2        col3
0     2     1  Somestuff2
1     2     1  Somestuff3

你试过什么吗?它对你来说执行得太慢了吗?@WayneWerner我不知道如何执行我描述的步骤。例如,如何列出前两列的唯一对?您尝试过什么吗?它对你来说执行得太慢了吗?@WayneWerner我不知道如何执行我描述的步骤。例如,如何列出前两列的唯一对?可以将
sample()
方法的参数从1更改为10(
.sample(n=10)
)。它将选择10对独特的配对(当然,假设至少有10对独特的配对)。这真是一个伟大而令人惊讶的答案!熊猫看起来很神奇。@eleanora是的,熊猫很棒。:)好的,我现在看到了性能问题。让我接受这个答案并询问另一个答案。您可以将
sample()
方法的参数从1更改为10(
.sample(n=10)
)。它将选择10对独特的配对(当然,假设至少有10对独特的配对)。这真是一个伟大而令人惊讶的答案!熊猫看起来很神奇。@eleanora是的,熊猫很棒。:)好的,我现在看到了性能问题。让我接受这个答案,再问另一个。