+ 1

To separate a part of dataframe in python

Hi everyone I have a cab file, in size 1000×11. I want to separate last column and put them another array and at the end I will have two arrays 1000×10 and 1×10. I did it I.loc, but it only show the 1×10 array and there is no change in initial array.

11th Dec 2020, 10:03 AM
Sarah
Sarah - avatar
1 Odpowiedź
+ 1
I suppose you have a pandas DataFrame df. The simplest thing you can do is to use iloc, which work very likely loc but with the indexes of the DataFrame. So, X = df.iloc[:, :-1].copy() will create the DataFrame X which is a copy of df except for the last column. In the same way you can define y = df.iloc[:, -1:].copy() The reason why you must use [:, -1:] instead of [:, -1] is that the latter version will create a Series instead of a DataFrame (with only one column)
3rd Jan 2021, 12:33 AM
Alberto Manzini
Alberto Manzini - avatar