I have several csv files of a name sequence where the numbers change
I need to call a function that takes all these file at once
I tried to do it as following:
names = ["orm_x_CXY_" str(i) ".csv" for i in range(0,40)]
The function is defined as following
import pandas as pd
import numpy as np
def getBpms(filename, ipage):
twiss = pd.read_csv(filename, skiprows=1,
names=['s_pos', 'beta_x', 'beta_y', 'dx', 'dy', 'closed_orbitx', 'closed_orbity', 'element_type', 'elements_strength'],
header=None, index_col=0)
s = twiss.s_pos
x = twiss.closed_orbitx
y = twiss.closed_orbity
etype = twiss.element_type
xs = []
ys = []
i=0
for i in range(len(etype)):
if(etype[i] == 'BPM'):
xs.append('BPM' str(i))
ys.append('BPM' str(i))
return np.array(xs), np.array(ys)
when i tried to call the function with "names" as input as following
x, y =getBpms(names, 0)
I seems to inter in an infinite loop and no output was shown.
CodePudding user response:
You need to iterate each file for filename in names:
with 'pd.read_csv()` ,
if you try to concatenate multiple csv's into one DataFrame then you could check this question: Import multiple csv files into pandas and concatenate into one DataFrame
import pandas as pd
import glob
path = r'C:\DRO\DCL_rawdata_files' # use your path
all_files = glob.glob(path "/*.csv")
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=0)
li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)
I hope its what you meant to do.
CodePudding user response:
I tried to do it as you mentioned as foloowing
li = [pd.read_csv(filename) for filename in names]
frame = pd.concat(li, axis=0, ignore_index=True)
Then i called the function again
x, y =getBpms(frame, 0)
But i got the error
argument of type 'method' is not iterable
CodePudding user response:
You should create a loop in the getBpms
function to read files separately pandas don't have multiple file reading attribute.
...
for file_name in filename_list:
twiss = pd.read_csv(...)
...