- 我试图把一个csv,读它作为熊猫数据帧。
- 该数据帧包含4行数字。
- 我想选择从数据帧数据的特定行。
- 在一个while循环,我想选择从数据框排随机,并把它比作一行我挑。
- 我希望它继续通过while循环,直到随机行运行,是100%等于之前我选的行。
- 然后我想While循环打破,我想它已经算花了多少尝试匹配的随机数。
这是我到目前为止有:
这是数据帧的一个示例:
A B C D
1 2 7 12 14
2 4 5 11 23
3 4 6 14 20
4 4 7 13 50
5 9 6 14 35
这是我努力的一个例子:
import time
import pandas as pd
then = time.time()
count = 0
df = pd.read_csv('Get_Numbers.csv')
df.columns = ['A', 'B', 'C', 'D']
while True:
df_elements = df.sample(n=1)
random_row = df_elements
print(random_row)
find_this_row = df['A','B','C','D' == '4','7','13,'50']
print(find_this_row)
if find_this_row != random_row:
count += 1
else:
break
print("You found the correct numbers! And it only took " + str(count) + " tries to get there! Your numbers were: " + str(find_this_row))
now = time.time()
print("It took: ", now-then, " seconds")
上面的代码给出了一个明显的错误......但我已经尝试了许多不同的版本,现在找到的find_this_row
数字,我只是不知道该怎么办了,所以我离开了这个尝试英寸
我想尽量避免使用特定指数为行,我试图找到,我宁愿只使用值来找到这个。
我使用df_elements = df.sample(n=1)
以随机选择的行。 这是为了避免使用random.choice
因为我不知道是否会工作或采取何种方式是更多的时间/内存使用效率,但我乐于接受意见的这一点。
在我的脑海里看似简单,随机选择数据行,如果它不,我想数据行匹配,保持随机选择数据行,直到它匹配。 但我似乎无法执行它。
任何帮助是非常感谢!
可以使用返回值np.ndarray
的shape=(1, 2)
,利用values[0]
以获得刚一维数组。
然后比较与所述阵列any()
import time
import pandas as pd
then = time.time()
df = pd.DataFrame(data={'A': [1, 2, 3],
'B': [8, 9, 10]})
find_this_row = [2, 9]
print("Looking for: {}".format(find_this_row))
count = 0
while True:
random_row = df.sample(n=1).values[0]
print(random_row)
if any(find_this_row != random_row):
count += 1
else:
break
print("You found the correct numbers! And it only took " + str(count) + " tries to get there! Your numbers were: " + str(find_this_row))
now = time.time()
print("It took: ", now-then, " seconds")
如何使用values
?
values
将返回值的列表。 然后你就可以轻松地比较两个列表。
list1 == list2
将返回的数组True
和False
,因为它比较了相应的列表的索引值。 您可以检查是否所有返回的值是True
下面是在同一时间测试一排的方法。 我们检查是否values
所选择的行等于采样的数值DataFrame
。 我们要求他们all
的比赛。
row = df.sample(1)
counter = 0
not_a_match = True
while not_a_match:
not_a_match = ~(df.sample(n=1).values == row.values).all()
counter+=1
print(f'It took {counter} tries and the numbers were\n{row}')
#It took 9 tries and the numbers were
# A B C D
#4 4 7 13 50
如果你想获得快一点点,你选择一个行,然后采样DataFrame
与更换很多次。 然后,您可以检查首次采样行等于你的采样DataFrame
,给你很多“尝试”,将采取在一个while循环,但在更短的时间如何。 循环防止因为它与放回抽样我们没有找到匹配的可能性不大的情况下,。
row = df.sample(1)
n = 0
none_match = True
k = 10 # Increase to check more matches at once.
while none_match:
matches = (df.sample(n=len(df)*k, replace=True).values == row.values).all(1)
none_match = ~matches.any() # Determine if none still match
n += k*len(df)*none_match # Only increment if none match
n = n + matches.argmax() + 1
print(f'It took {n} tries and the numbers were\n{row}')
#It took 3 tries and the numbers were
# A B C D
#4 4 7 13 50
一对夫妇提示的第一位。 此行不为我工作:
find_this_row = df['A','B','C','D' == '4','7','13,'50']
有2个原因:
- 缺少“'”后,'13
- df是一个数据帧(),因此,使用密钥象下面不支持
DF [ 'A', 'B', 'C', 'D' ...
无论是使用键返回一个数据框():
df[['A','B','C','D']]
或作为系列():
df['A']
既然你需要整行多列做到这一点:
df2.iloc[4].values
阵列([ '4', '7', '13', '50'],D型细胞=对象)
这样做与您的样本行:
df2.sample(n=1).values
需要的行之间的比较对所有()元素/列进行:
df2.sample(n=1).values == df2.iloc[4].values
阵列([[真,假,FALSE,FALSE]])
与添加。所有()如下所示:
(df2.sample(n=1).values == df2.iloc[4].values).all()
返回
真假
全部一起:
import time
import pandas as pd
then = time.time()
count = 0
while True:
random_row = df2.sample(n=1).values
find_this_row = df2.iloc[4].values
if (random_row == find_this_row).all() == False:
count += 1
else:
break
print("You found the correct numbers! And it only took " + str(count) + " tries to get there! Your numbers were: " + str(find_this_row))
now = time.time()
print("It took: ", now-then, " seconds")