0

I have a pandas DataFrame like this:

       min      max
  1.  10186     10186
  2.  10197     10197
  3.  10199     11142
  4.  11144     11654
  5.  11656     13498
  6.  13500     13977
  7.  13979     14442
  8.  14445     14446
  9.  14448     14449

Can I get all the values between each min and max values inclusive of these values?

Example Output:

10186
10197
10199, 10200, 10201, 10202 etc.

How can I achieve this? This is a big file containing more than 10k records. Any headstart will also help. I currently got nothing. My present code:

import pandas as pd
avrange = pd.read_excel('C:\\Users\\Desktop\\apvdcorrection.xlsx')

df1 = pd.DataFrame(avrange, columns = ['avmin', 'avmax'])
df2 = df1[df1.avmin != 0]
df2 = df2[df2.avmax != -1]
df2 = df2.astype(int)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Karan Gupta
  • 529
  • 2
  • 7
  • 21

1 Answers1

1

You can. Here's a list comprehension to do it. You really cannot vectorize this due to the inherent nature of this problem.

df['values'] = [
    list(range(x, y + 1)) for x, y in zip(df['min'], df['max'])
]
cs95
  • 379,657
  • 97
  • 704
  • 746
  • I am kinda new to python. Vectorize means? And can I store these values in a separate list? – Karan Gupta Apr 05 '18 at 09:26
  • @KaranGupta You can do certain operations without having to write a loop, because those operations are "vectorised". Note that `df.apply` actually is a wrapper around the loop, and is slower than a pure list comprehension. – cs95 Apr 05 '18 at 09:28
  • @KaranGupta Did you try `mylist = [list(range(x, y + 1)) for x, y in zip(df['min'], df['max'])]`? – cs95 Apr 05 '18 at 09:29
  • Yeah,it created a list of lists. I want to convert it into a single list of these values. – Karan Gupta Apr 05 '18 at 09:31
  • @KaranGupta: that is a different question, like this: [Making a flat list out of list of lists in Python](https://stackoverflow.com/q/952914/4464653) – Skandix Apr 05 '18 at 09:35