我使用以下代码将.csv文件的列附加到pandas Dataframe/Series对象的列表中
process_time_dfs = []
status_dfs = []
for string in filestrings:
#read in command with apropriate delimiter and engine starting from a nmc-specific line in the excel doc
EIS_TS_csv = pd.read_csv(DATA_DIR.joinpath(string), engine = 'python', delimiter = ';', skiprows = startposition)
#drop [descriptor] row
EIS_TS_csv.drop(index = 0, axis = 0, inplace = True)
#add to dataframe
status_dfs.append(EIS_TS_csv['Status'])
process_time_dfs.append(EIS_TS_csv['Progr. Zeit'].str.replace('.', '').astype(float))此代码适用于大多数.csv文件,但不适用于某些特定文件,因为其中的列'Progr. Zeit'不包含string对象。所以我想我可以用if子句使代码适应这种情况:
process_time_dfs = []
status_dfs = []
for string in filestrings:
#read in command with apropriate delimiter and engine starting from a nmc-specific line in the excel doc
EIS_TS_csv = pd.read_csv(DATA_DIR.joinpath(string), engine = 'python', delimiter = ';', skiprows = startposition)
#drop [descriptor] row
EIS_TS_csv.drop(index = 0, axis = 0, inplace = True)
#add to dataframe
status_dfs.append(EIS_TS_csv['Status'])
if EIS_TS_csv['Progr. Zeit'].dtype == object:
process_time_dfs.append(EIS_TS_csv['Progr. Zeit'].str.replace('.', '').astype(float))
else:
process_time_dfs.append(EIS_TS_csv['Progr. Zeit'].astype(float))在我看来,这似乎是一个很小的更改,应该可以很好地工作,但它会产生以下错误:
>>> process_time_dfs.append(EIS_TS_csv['Progr. Zeit'].str.replace('.', '').astype(float))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'process_time_dfs' is not defined我不知道这是怎么发生的,因为名字不被识别的列表是以与以前相同的方式定义的。请帮我理解这里出了什么问题!
发布于 2021-04-30 21:31:01
我认为你需要改变你输入路径的方式。
path = r'/usr/local/bin/python3 "/Users/gnthr/Desktop/EISTSAnalysisCalendric.py"'我只是添加了path,使其成为一个变量。我不知道你这样做是为了什么。
https://stackoverflow.com/questions/67333170
复制相似问题