0

I have dataframe A that is 43,249 rows x 10 cols. I have a list of random indices. I want to extract into their own dataframe. I have looked at other methods, and they do not seem to work.

I have tried:

list = [1 5 6 9 11 17 ... 564 985 2545...]

dfB = dfA.pop(list)

and

dfB = dfA.drop(list)

and

dfB = dfA.loc(list)
TypeError: unhashable type: 'list'

I thought you could do this without a for loop.

  • Are you sure your list is like `[1,2]` not `[[1,2]]`? – Ynjxsjmh Mar 10 '23 at 00:32
  • @Ynjxsjmh yes, `Output exceeds the size limit. Open the full output data in a text editor [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,` – Tony Sirico Mar 10 '23 at 00:34
  • 1
    I know, you need use `df.loc[list]`(square bracket not the round), but you'd better rename `list` to another name since it's a keyword in python. If your index is not like `0,1,2`, you need use `df.iloc[list]`. – Ynjxsjmh Mar 10 '23 at 00:35
  • @Ynjxsjmh ok, your fix worked, but now it corrupted the data. The original DF's first column, each cell contained a matrix which would get passed through networkx. Now in DFB, the matrix cannot be put in networkx `NetworkXError: Input is not a valid edge list` – Tony Sirico Mar 10 '23 at 02:59
  • @Ynjxsjmh found out why. When we do loc or iloc, even if each cell in the row are different dtypes, loc and iloc will change everything to one dtype, object. Is there a work around? – Tony Sirico Mar 10 '23 at 03:08
  • This seems another question, you can ask a new question and add more detail to this. I don't think the object dtype would cause the matrix couldn't be fed into the networkx. – Ynjxsjmh Mar 10 '23 at 05:36

0 Answers0