3

Since my previous question went unanswered, I'll try again more simply.

I have a program which makes a list containing row and column indexes:

a = [0, 0, 1, 1] # row
b = [0, 1, 0, 1] # column

This can also be formatted as lists of list:

a = [[0,0], [0,1], [1,0], [1,1]]

Dataframe:

      Name  2544  2543  2542
0     DR01    13    16    23
1     DR02    15    27    12
2     DR03    16    18    13
3     DR04    17    92    56

Objective is to obtain the values from the dataframe mentioned based on indexes in list and create a new dataframe. I tried a for loop but that didn't work and was very slow.

What it should look like:

      Name  2544
0     DR01    13
1     DR02    15

Update:

Here is what I have tried:

In[96]: df = importDialog.df.iloc[a,b]
In[96]: print(df)

Out[96]:
      Name  2544    Name  2544
0     DR01    13    DR01    13
1     DR02    15    DR02    15

Also, when I try showing the complete dataset in a table:

MemoryError: Unable to allocate 9.06 GiB for an array with shape (34749, 34992) and data type object
6
  • Can you please post what you tried and what the output was? Commented Feb 20, 2020 at 21:03
  • Are the indexes always create a rectangular shape? Commented Feb 20, 2020 at 21:05
  • 1
    @GlenvillePecor Done! Commented Feb 20, 2020 at 21:10
  • @QuangHoang I'm planning to add a condition that if the shape is not rectangular, an exception is raised. Could you help me with that? Commented Feb 20, 2020 at 21:13
  • How does your a with six pairs yield a 2x2 dataframe? Commented Feb 20, 2020 at 21:13

1 Answer 1

1

Assuming your checked that a,b constitute a rectangular shape:

a = [0, 0, 1, 1] # row
b = [0, 1, 0, 1] # column

df.iloc[sorted(set(a)),sorted(set(b))]

Output:

   Name  2544
0  DR01    13
1  DR02    15
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a ton, been breaking my head with this for almost 2 days. Two more thing, is there a way to do this if the shape is not rectangular? and how would I check if the shape is not rectangular?
len(a) == len(set(a)) * len(set(b))
That makes sense. Thanks again. Would vote your answer up, but I don't have enough rep.
Also, you need to check if a,b have same length, which might not be the case from your error message.

Your Answer

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