1

So I am trying to add each equation only once to my dictionary and put in in a Dataframe. However, every instance in the for loop is being added to the Dataframe multiple times.

below is a sample output.

                                               Eq ADD                                             Eq SUB
0   (16 + 100 =     , 86 + 77 =     , 8 + 75 =    ...  (78 - 79 =     , 72 - 84 =     , 25 - 78 =    ...
1   (16 + 100 =     , 86 + 77 =     , 8 + 75 =    ...  (78 - 79 =     , 72 - 84 =     , 25 - 78 =    ...
2   (16 + 100 =     , 86 + 77 =     , 8 + 75 =    ...  (78 - 79 =     , 72 - 84 =     , 25 - 78 =    ...
3   (16 + 100 =     , 86 + 77 =     , 8 + 75 =    ...  (78 - 79 =     , 72 - 84 =     , 25 - 78 =    ...
4   (16 + 100 =     , 86 + 77 =     , 8 + 75 =    ...  (78 - 79 =     , 72 - 84 =     , 25 - 78 =    ...
5   (16 + 100 =     , 86 + 77 =     , 8 + 75 =    ...  (78 - 79 =     , 72 - 84 =     , 25 - 78 =    ...

Below is my code.

import random
import pandas as pd

_dict_add = {}
_dict_sub = {}

for i in range(0, 50):
    value1_add = random.randint(0, 100)
    value2_add = random.randint(0, 100)
    eq_add = {str(value1_add) + " + " + str(value2_add) + " =     ": value1_add + value2_add}
    _dict_add.update(eq_add)

    value1_sub = random.randint(0, 100)
    value2_sub = random.randint(0, 100)
    eq_sub = {str(value1_sub) + " - " + str(value2_sub) + " =     ": value1_sub - value2_sub}
    _dict_sub.update(eq_sub)

dataframe = pd.DataFrame({
    'Eq ADD': _dict_add.keys(),
    'Eq SUB': _dict_sub.keys()
})
print(dataframe)

Why does this happen? I would assume my loop is adding one instance only once. But that is not the case in my Dataframe. How do I prevent this?

EXPECTED OUTPUT

                 0               1
0   79 + 15 =       26 - 36 =
1   93 + 42 =       70 - 17 =
2    3 + 27 =       15 - 51 =
3   70 + 72 =       40 - 27 =
4    6 + 99 =       88 - 35 =
5   30 + 10 =       97 - 84 =
1
  • Can you give an example of your expected output? Commented Feb 7, 2020 at 1:23

2 Answers 2

1

The problem was that when constructing a DataFrame, dictionaries have to be passed in '[]'.

The below code accomplished the task.

import random
import pandas as pd

_dict_add = {}
_dict_sub = {}

for i in range(0, 50):
    value1_add = random.randint(0, 100)
    value2_add = random.randint(0, 100)
    eq_add = {str(value1_add) + " + " + str(value2_add) + " =     ": value1_add + value2_add}
    _dict_add.update(eq_add)

    value1_sub = random.randint(0, 100)
    value2_sub = random.randint(0, 100)
    eq_sub = {str(value1_sub) + " - " + str(value2_sub) + " =     ": value1_sub - value2_sub}
    _dict_sub.update(eq_sub)

dataframe = pd.DataFrame([_dict_add.keys(), _dict_sub.keys()])
print(dataframe.transpose())
Sign up to request clarification or add additional context in comments.

1 Comment

It's a weird result you got there regardless. If somehow the first one repeats, then I'd see that as a bug.
1

I'm assuming that you want to use the list of keys of both dictionaries as column values for your pandas dataframe. What is causing your error to happen is that in Python >= 3.3, dict.keys() returns an object of class dict_keys. Pandas dataframes take lists, so you need to convert these dict_keys objects into lists.

dataframe = pd.DataFrame({
    'Eq ADD': list(_dict_add.keys()),
    'Eq SUB': list(_dict_sub.keys())
})

Output:

            Eq ADD          Eq SUB
0    3 + 94 =       80 - 40 =
1   87 + 10 =       31 - 79 =
2   27 + 70 =       19 - 71 =
3   31 + 56 =       81 - 34 =
4   79 + 84 =       20 - 64 =
5   46 + 94 =       75 - 36 =
6   69 + 88 =       11 - 30 =
7   62 + 35 =        41 - 8 =
8    98 + 3 =       81 - 69 =
9   92 + 83 =       28 - 96 =
10   16 + 0 =       83 - 58 =
11  94 + 11 =        6 - 48 =
12  38 + 60 =       79 - 70 =
13  22 + 43 =       30 - 22 =
14  25 + 67 =       10 - 88 =
15  53 + 67 =       39 - 10 =
.
.
.
45  66 + 29 =       50 - 40 =
46   21 + 1 =       61 - 78 =
47  27 + 23 =       58 - 94 =
48   25 + 8 =       55 - 83 =
49  37 + 76 =       71 - 49 =

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.