简体   繁体   中英

How to read a specific column of csv file using python

I am new at Scikit-Learn and I want to convert a collection of data which I have already labelled into a dataset. I have converted the .csv file of the data into a NumPy array, however one problem I have run into is to classify the data into training set based on the presence of a flag in the second column. I want to know how to access a particular row, column of a .csv file using the Pandas Utility Module. The following is my code:

    import numpy as np
    import pandas as pd
    import csv
    import nltk
    import pickle
    from nltk.classify.scikitlearn import SklearnClassifier
    from sklearn.naive_bayes import MultinomialNB,BernoulliNB
    from nltk.classify import ClassifierI
    from statistics import mode




    def numpyfy(fileid):
         data = pd.read_csv(fileid,encoding = 'latin1')
         #pd.readline(data)
         target = data["String"]
         data1 = data.ix[1:,:-1]
         #print(data)
         return data1
    def learn(fileid):
         trainingsetpos = []
         trainingsetneg = []
         datanew = numpyfy(fileid)
         if(datanew.ix['Status']==1):
            trainingsetpos.append(datanew.ix['String'])
         if(datanew.ix['Status']==0):
            trainingsetneg.append(datanew.ix['String'])

    print(list(trainingsetpos))

You can use boolean indexing to split the data. Something like

import pandas as pd


def numpyfy(fileid):
    df = pd.read_csv(fileid, encoding='latin1')
    target = df.pop('String')
    data = df.ix[1:,:-1]
    return target, data


def learn(fileid):
    target, data = numpyfy(fileid)
    trainingsetpos = data[data['Status'] == 1]
    trainingsetneg = data[data['Status'] == 0]

    print(trainingsetpos)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM