[英]Finding the boolean from a table of random integers compared to a list
Inputs are:输入是:
A Panda Dataframe with 500 columns and 10 lines, which contains a series of random integers comprised between 0 and 10000 (included)具有 500 列和 10 行的 Panda Dataframe,其中包含一系列介于 0 和 10000(包括在内)之间的随机整数
A list of 10 random integers comprised between 0 and 10000包含 0 到 10000 之间的 10 个随机整数的列表
The output I am looking for is:我正在寻找的输出是:
A Panda Dataframe with 500 columns and 10 lines, which gives the Boolean true or false depending if the element from the x-th line is above (true) or below (false) the number which is the x-th element of the list具有 500 列和 10 行的 Panda Dataframe,它给出布尔值 true 或 false,具体取决于第 x 行中的元素是高于(true)还是低于(false)列表的第 x 个元素的数字
I was able to solve this in excel using the following functions:我能够使用以下函数在 excel 中解决这个问题:
Is there an elegant way of producing this solution in python?有没有一种优雅的方式在 python 中产生这个解决方案? I am still a beginner learning more about python.
我仍然是一个初学者,正在学习更多关于 python 的知识。
Thank you for the help感谢您的帮助
Update: Using MSS's solution, this is my final code.更新:使用 MSS 的解决方案,这是我的最终代码。 Could you please tell me if there are any mistakes in my code?
你能告诉我我的代码是否有任何错误吗?
import numpy as np
import pandas as pd
import random
df = pd.DataFrame(np.random.randint(0,10000,size=(10, 500)))
df.head
list = random.sample(range(10000), 10)
print(list)
a = df.to_numpy()
b = np.array(list)
res = pd.DataFrame(a > b[:,None], index= df.index, columns=df.columns)
print(res)
Thank you for the help感谢您的帮助
You can do it in this way using numpy.您可以使用 numpy 以这种方式执行此操作。
a = df.to_numpy() # Dataframe of shape (10,500)
b = np.array(your_list) # your_list contains 10 random numbers >=1 and <=10000
res = pd.DataFrame(a > b[:,None], index= df.index, columns=df.columns)
Lets explain using a smaller dataframe having 3 lines and 5 columns and a list having 3 numbers.让我们使用一个具有 3 行和 5 列的较小数据框和一个具有 3 个数字的列表来解释。 All numbers are random between 1-9.
所有数字都是 1-9 之间的随机数。
inter = np.array([[1,2,3,5],[4,5,6,1],[7,8,9,5]])
df = pd.DataFrame(inter)
your_list = [3,6,7]
The output obtained after applying above code is:应用上述代码后得到的输出为:
0 1 2 3
0 False False False True
1 False False False False
2 False True True False
Hence solution is correct.因此解决方案是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.