I want to read all the csv
files in my input_path
as a dataframe df
. My code raises NameError: name 'df' is not defined
error.
import os
import pandas as pd
input_path = "../samples/input_data/"
# Open all the subfolders within path
for root, dirs, files in os.walk(input_path):
for file in files:
with open(os.path.join(root, file), "r") as data:
df = pd.read_csv(data)
print(df)
Traceback:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/tmp/ipykernel_2834/400103009.py in <module>
----> 1 print(df)
NameError: name 'df' is not defined
import os
import pandas as pd
input_path = "../samples/input_data/"
# Open all the subfolders within path
for root, dirs, files in os.walk(input_path):
for file in files:
with open(os.path.join(root, file), "r") as data:
df = pd.read_csv(data)
print(df)
I think this should work. When you come out of for loop python doesnt know what df is. So if want to print df or do some other actions then do it within the for loop itself. You can also use a function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.