在While循环中从Pandas数据框中查找特定的数据行 [英] Find specific Row of Data from Pandas Dataframe in While Loop

查看:559
本文介绍了在While循环中从Pandas数据框中查找特定的数据行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我正在尝试使用csv,并将其作为Pandas Dataframe读取.
  2. 此数据框包含4行数字.
  3. 我想从数据框中选择特定的数据行.
  4. 在While循环中,我想从Dataframe中选择一个随机行,并将其与我选择的行进行比较.
  5. 我希望它继续在while循环中运行,直到该随机行等于我之前选择的行的100%.
  6. 然后,我希望While循环中断,并希望它计算出与随机数匹配所需的尝试次数.

这是我到目前为止所拥有的:

Here's what I have so far:

这是数据框的示例:

    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

这是我努力的一个例子:

Here is an example of my efforts:

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数字,以至于我什至不知道该怎么做,所以我放弃了这一尝试.

The above code gives an obvious error... but I have tried so many different versions now of finding the find_this_row numbers that I just don't know what to do anymore, so I left this attempt in.

我想避免的是对要查找的行使用特定的索引,我宁愿仅使用值来查找该行.

What I would like to try to avoid is using the specific index for the row I am trying to find, I would rather use just the values to find this.

我正在使用df_elements = df.sample(n=1)随机选择一行.这样做是为了避免使用random.choice,因为我不确定这是否行得通,或者哪种方式更节省时间/内存,但我也愿意就此提出建议.

I am using df_elements = df.sample(n=1) to select a row at random. This was to avoid using random.choice as I was not sure if that would work or which way is more time/memory efficient, but I'm open to advice on that as well.

在我看来,随机选择一行数据似乎很简单,如果它与我想要的数据行不匹配,请继续随机选择数据行直到匹配为止.但是我似乎无法执行它.

In my mind it seems simple, randomly select a row of data, if it doesn't match the row of data that I want, keep randomly selecting rows of data until it does match. But I can't seem to execute it.

非常感谢任何帮助!

推荐答案

您可以使用返回shape=(1, 2)np.ndarray值,使用values[0]来获取一维数组.

You can use values which returns np.ndarray of shape=(1, 2), use values[0] to get just 1D array.

然后将数组与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")

这篇关于在While循环中从Pandas数据框中查找特定的数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆