I am trying to read 3 CSV files into 3 pandas DataFrame. But after executing the function the variable seems not available. Tries to create a blank data frame outside the function and read and set the frame in the function. But the frame is blank.
# Load data from the csv file
def LoadFiles():
x = pd.read_csv('columns_description.csv', index_col=None)
print("Columns Description")
print(f"Number of rows/records: {x.shape[0]}")
print(f"Number of columns/variables: {x.shape[1]}")
LoadFiles()
x.head()
Python Notebook for above code with Error
In the second approach, I am trying to create a new data frame with some consolidated information from the dataset. The issue reappears as the variable seems to be no longer available.
# Understand the variables
y = pd.read_csv('columns_description.csv', index_col=None)
def refresh_y():
var_y = pd.DataFrame(columns=['Variable','Number of unique values'])
for i, var in enumerate(y.columns):
var_y.loc[i] = [y, y[var].nunique()]
refresh_y()
Screenshot with error code and solution restructuring in the function
I am a bit new to Python, The code is a sample and does not represent actual data and in the function, an example is with a single column. I have multiple columns to refresh in this derived data set based on changes further hence the function approach.