1

I want to get all the fields from the csv which are numerical fields and store those field in an array so that i can perform mathematical operations. I can get the data types but not able to restrict. I am very new to python scripting, please help

Edit: I have added one sample row

enter image description here

so here F1 and F3 are the numerical fields. So i want to keep these two field names in an array variable FieldNames=["F1","F3"]

import csv
import pandas as pd
import numpy as np

data = pd.read_csv(r'C:\Users\spanda031\Downloads\test_19.csv')
print(data.dtypes)
with open(r'C:\Users\spanda031\Downloads\test_19.csv') as f:
    d_reader = csv.DictReader(f)

    #get fieldnames from DictReader object and store in list
    headers = d_reader.fieldnames
    print(headers)
    for line in headers:
        #print value in MyCol1 for each row
        print(line)        
        v3=np.array(data[line])
2
  • Please add a sample csv row Commented Nov 15, 2018 at 13:48
  • also provide your expected output Commented Nov 15, 2018 at 13:49

2 Answers 2

2

select_dtypes

You can use np.number or, as indicated in the docs, 'number' to select all numeric series:

# read csv file
df = pd.read_csv('file.csv')

# subset dataframe to include only numeric columns
df = df.select_dtypes(include='number')

# get column labels in array
cols = df.columns.values

# extract NumPy array from dataframe
arr = df.values

Notice there's no need for the csv module, as Pandas can read csv files via pd.read_csv.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your reply. I just want to store column names in the array not the values of those columns. You code is writing column values into the array
0

isdigit can be used to check whether only numeric value is present in any column of dataframe in Python. Say the column name is Measure, then, you can write

df['Measure_isdigit'] = map(lambda x: x.isdigit(), df['Measure'])

print df

1 Comment

Mean while, if you are new as you said, straight away starting with Pandas will get you in trouble. You are using numpy, dictionary etc.How you are new..? Go through operations on lists in detail...You'll learn how to use isdigit function well...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.