0

I have the following code in which I read CSV files and get a graph plotted:

import numpy as np
import matplotlib.pyplot as plt
import scipy.odr
from scipy.interpolate import interp1d
plt.rcParams["figure.figsize"] = (15,10)

def readPV(filename="HE3.csv",d=32.5e-3):
    t=np.genfromtxt(fname=filename,delimiter=',',skip_header=1, usecols=0)
    P=np.genfromtxt(fname=filename,delimiter=',',skip_header=1, usecols=1)
    V=np.genfromtxt(fname=filename,delimiter=',',skip_header=1, usecols=2,filling_values=np.nan)
    V=V*np.pi*(d/2)**2
    Vi= interp1d(t[~np.isnan(V)],V[~np.isnan(V)],fill_value="extrapolate")
    V=Vi(t)
    return P,V,t

P,V,t=readPV(filename="HE3.csv")

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(V,P,'ko')
ax.set_xlabel("Volume")
ax.set_ylabel("Pressure")
plt.show()

From this code, the following graph is made: Graph from CSV file

The CSV file has several data points in one column, separated by commas; I want to know how to pick a range of columns to read, instead of all of them.

  • Please provide some lines of example data to show what it is you're trying to deal with. Also, you're already using code that reads specific columns only - in fact, you're reading the entire file three times in a row, reading it a column at a time. Instead of reading it three times, you could have used a tuple for `usecols` to read it in one go. – Grismar Nov 25 '20 at 20:52
  • 1
    What exactly do you want to achieve? Faster loading or only using a part of the data? In most cases, you always have to read the whole file and can only afterwards discard columns or rows, e. g. with slicing. – E. Körner Nov 25 '20 at 20:53
  • 2
    are you able to use external libraries like pandas? – Kunal Shah Nov 25 '20 at 20:53
  • construct your own file-like-object and process the CSV file and simulate a smaller CSV file with less columns – rioV8 Nov 25 '20 at 21:55
  • https://stackoverflow.com/questions/23853553/python-pandas-how-to-read-only-first-n-rows-of-csv-files-in/23853569 – Kunal Shah Nov 29 '20 at 02:39

0 Answers0