Split CSV file Columns into multiple files with iloc

Viewed 508

I have a CSV file which contains 98 columns(for now) which have columns from other files I just want to split the other files' columns into new CSV file just like this...

I have a csv file uploaded here with name GenerateAndSplitSeedFile(Parts_skills_predict).csv, look at it if you want.

data =pd.read_csv('MergedSeedData(Parts_skills_Durations_fix).csv')
print(len(data.columns))
data1 = data.drop(data.iloc[:, 64:], axis=1)
print(data1)
data1.to_csv('data1file.csv', index=False)

Here I'm reading the CSV file and I'm getting the only 64 columns from 98 columns and writing it to the CSV file.

Again same operations for other split, here I need to get columns from (0 - 8) and from (64 - 83) for that I have done here is:

data22 = data.drop(data.iloc[:, 8:64], axis=1)
data2 = data22.drop(data22.iloc[:, 19:], axis=1)
# print(data2)
data2.to_csv('datafile.csv', index=False)

Here I have splitted the columns from 0-8 and 64-98 in first split(data22) and then drop the extra columns from data2

Question is, how to make the split for data2 in single line I want to split the columns from 0-8,64-83 from main file which total of 98 columns I need to write in a single line...

1 Answers

There are multiple ways of doing it:

1.) np.concatenate and np.arange

#selecting 0-8,64-83
data.iloc[:, np.concatenate((np.arange(0,8),np.arange(64,83)),axis=0))]
#This is generate a list of column indexes you want to choose

2) np.r_

data.iloc[:, np.r_[0:8,64:83]]
Related