1

I have a pandas data frame with hundreds of entries and an array of random entries in the array. For example:

import pandas as pd
list1 = [13,2,32,34,15,7,19]
list2 = [15,65,95,9,90,88,10]
df1 = pd.DataFrame(list1)
df2 = pd.DataFrame(list2)

cols = [df1, df2]  
df1.loc[:, cols]

and I have another array called

M =[1, 2, 5, 6, 9] 

where these are the indexes of the pandas data frame I want, is there a way to create a new table that picks out only the rows that match the index given by the array M?

Kdav
  • 21
  • 2
  • Possible duplicate of [Selecting a row of pandas series/dataframe by integer index](https://stackoverflow.com/questions/16096627/selecting-a-row-of-pandas-series-dataframe-by-integer-index) – ayorgo Dec 09 '18 at 00:27
  • 1
    `df1.iloc[M]` should help. – ayorgo Dec 09 '18 at 00:29
  • @ayorgo when I try this I get a "int() argument must be a string, a bytes-like object or a number, not 'set'" error. – Kdav Dec 09 '18 at 00:42

1 Answers1

0
import pandas as pd
list1 = [13,2,32,34,15,7,19]
df1 = pd.DataFrame(list1)

M =[1, 2, 5, 6] 

df1[df1.index.isin(M)]

Note that in your problem statement, cols is a list of dataframes, not a two-column dataframe. I am not sure if that was not clear from your code and question.

Evan
  • 2,121
  • 14
  • 27
  • This is something I have tried but I get an error "Buffer has wrong number of dimensions (expected 1, got 0)". – Kdav Dec 09 '18 at 01:23
  • Can you post your actual code, and the actual error message? When you run the exact code I posted above, do you get an error? – Evan Dec 09 '18 at 01:25