extract certain columns and create new data file - Ariel0128/pythonlearning GitHub Wiki


How to extract certain columns from csv and create a new csv

  1. In python, the counting begins from 0, so the number of selected columns should be -1:
  2. [] for array.
  3. Notice how to create a new array and establish relation between 2 arrays.
  4. xxx.to_csv : to create new csv file
ncol = [2, 114, 168, 191, 331, 352, 387, 399, 875, 969, 973, 1023, 1041, 1048, 1059, 1061, 1090, 1109, 1119, 1141, 1144,
        1147, 1154, 1155, 1166, 1199, 1208, 1219, 1230, 1244, 1255, 1279, 1310, 1328, 1345, 1363, 1374, 1386, 1392,
        1395, 1400, 1423, 1424, 1425, 1436, 1456, 1484, 1514, 1533, 1542, 1555, 1556, 1559]

new_col = []
for i in ncol:
    new_col.append(i - 1)

predictors = rawdata.iloc[:, new_col]

predictors.to_csv('B1.csv')