问题
I am writing a Python code using pandas that will open a .csv file and read some parameters that will be used later as input for another module. Along the parameters the code must read, there are locations (paths) of other .csv files in my internal network that contain data that must be incorporated later on to the final output. My problem is opening those files; unless I define explicitly the path (instead of using a reference variable that will allow me to loop over all the .csv files my final code needs), I get the ValuError: Invalid file path or buffer object type: .
I tried adding single and double quotation marks to the paths, but that didn't help. Can somebody help me to figure out what I am doing wrong?
Below are pieces of my code that hopefuly will help to clarify the issue.
Thanks in advance for your help!
Root_path = c_input_df.loc["HF Modeling folder full path"]
Input_path = Root_path + c_input_df.loc["FO_Input_Files folder name & location"]
Next cell
Input_path
Parameters C:/Users/Pegaso/AnacondaProjects/2.-SuperFO/2.Projects/Client_ABC/Internal Data/HF Modeling/FO_Input_Files/1.-Model_13102017/UNI-09_original/
dtype: object
Next cell
well_name
Parameters 'UNI-09'
Name: Well name, dtype: object
#those two strings (Input path and well_name) are used to tell the path and part of the name of the .csv file to open
Next cell
#This is the prefered method to read the dataframe:
FT_file = pd.read_csv(Input_path + "FT-" + well_name + ".csv")
#But it gives the following error:
ValueError: Invalid file path or buffer object type: <class 'pandas.core.series.Series'>
Next cell
#Since the method above gives an error, I used the explicit path:
FT_file = Input_path + "FT-" + well_name + ".csv"
FT_file
Parameters C:/Users/Pegaso/AnacondaProjects/2.-SuperFO/2.Projects/Client_ABC/Internal Data/HF Modeling/FO_Input_Files/1.-Model_13102017/UNI-09_original/FT-UNI-09.csv
dtype: object
#When I try the path directly in the pd.read_csv function, it reads the file
FT_file = pd.read_csv("C:/Users/Pegaso/AnacondaProjects/2.-SuperFO/2.Projects/Client_ABC/Internal Data/HF Modeling/FO_Input_Files/1.-Model_13102017/UNI-09_original/FT-UNI-09.csv")
FT_file
Par_1 Par_2 Par_3
0 Units_1 Units_2 Units_3
1 6630 2448.270301 3659.999055
2 6647.99982 2448.270301 3659.999055
I hope I made myself understood, if that's not the case, please let me know and I will try to explain the issue in more detail.
Rgds,
Pegaso
回答1:
Not sure yet why, but the problem was in this line of code:
Root_path = c_input_df.loc["HF Modeling folder full path"]
If I use the method astype(str).Parameters
root_path = c_input_df.loc["HF Modeling folder abs path"].astype(str).Parameters
I get the result I was looking for, just the string, let see it:
Before:
root_path = c_input_df.loc["HF Modeling folder abs path"] #.astype(str).Parameters
print root_path
Parameters L:/Data/Jose/2.-SuperFO_testing/1612-02_University_9-319H_FleurDeLis/Internal Data/HF Modeling/
Name: HF Modeling folder abs path, dtype: object
...and when I add the parameters at the end
root_path = c_input_df.loc["HF Modeling folder abs path"] .astype(str).Parameters
print root_path
L:/Data/Jose/2.-SuperFO_testing/1612-02_University_9-319H_FleurDeLis/Internal Data/HF Modeling/
I got my problem solved, but I would like to understand better why this behavior when importing data from dataframes.
Rgds,
Pegaso
来源:https://stackoverflow.com/questions/46760188/problems-opening-a-path-in-python