0

I have a function that accepts two arguments. for instance:

def sum(a,b):
   s = a+b
   return s

and I plan to make a dataframe using following pseudocode:

for a in range(1,10):
  for b in range(100,200):
       add(a,b)

and then i want to create a dataframe like:

DF:
A     B     Sum
1     100   101
1     101   102 
1     102   103
1     103   104
...

I knwo i can do it in one line if I ahve only one forloop like :

df = pd.DataFrame([sum(a,b) for a,b in zip(range(1,10),range(100.200))],
                  columns=['A', 'B', 'Sum'])

However this will not repeat A for every B. How can i add a for loop inside another in one line?

Kathan Vyas
  • 355
  • 3
  • 16
  • `pd.DataFrame([(a,b,sum(a,b)) for b in range(100,200) for a in range(1,10)],columns=['A', 'B', 'Sum'])` – BENY Jun 18 '21 at 20:30
  • You just want `[sum(a,b) for a in range(1, 10) for b in range(100, 200)]` see the linked duplicate, the word you are looking for is "list comprehension" – juanpa.arrivillaga Jun 18 '21 at 20:31

0 Answers0