Why will this not list the directory – "2023"?
import pandas as pd
df = pd.DataFrame()
directory = "C:\\Users\\Downloads\\FL\\2023"
df["Directory"] = os.path.basename(directory)
df
>Solution :
To get the result you want
Here you want to create a new column in a DataFrame with no existing rows. You can do this by assigning a list to the new column:
df["Directory"] = [os.path.basename(directory)]
Result:
Directory
0 C:\Users\Downloads\FL\2023
What the original code did
Your original syntax (where you assign a single value) has the effect of setting that value for every row in the dataframe. In your example that does nothing, because there are no existing rows. However, contrast it with a situation where the dataframe already contains some rows:
import pandas as pd
import os
df = pd.DataFrame([1, 2, 3], columns=["foo"])
directory = "C:\\Users\\Downloads\\FL\\2023"
df["Directory"] = os.path.basename(directory)
print(df)
Here, the directory name would be propagated to each of the existing rows:
foo Directory
0 1 C:\Users\Downloads\FL\2023
1 2 C:\Users\Downloads\FL\2023
2 3 C:\Users\Downloads\FL\2023