+ 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.
1 Answer
+ 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)