Home > Blockchain >  Split Dataframs into its Columns
Split Dataframs into its Columns

Time:01-16

I am writing the code I have 461 columns in my dataset and I don't know the names of that column so I need to split those columns

i.e [0,1,2,3,4,.......,461] columns

I want to split into 2 data sets

i.e X = [0,1,2,3,4,......,460] and Y = [461]

import pandas as pd
dataset = pd.read_csv('dataset.csv')
dataset.head()
X = dataset.iloc[0:460]
#or 
X = dataset.filter(dataset[:460] , axis=1)
Y = dataset.iloc[:-1]

CodePudding user response:

You could read the whole dataset as X and then just pop one column and assign it to Y. pop does remove the column from the df and at the same time it returns the popped column.

import pandas as pd
X = pd.read_csv('dataset.csv')
Y = X.pop(X.columns[-1])

CodePudding user response:

import pandas as pd

dataset = pd.read_csv(r'Drive:\\path\\file.csv')
# Select all but last column
split = X[X.columns[:460]]
# Select last column
last_col= X[X.columns[-1:]]
  •  Tags:  
  • Related