Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List directory name in dataframe in python

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading