0

I have one Information csv , I used below to filter some specific name(Rony) from a column(Names) like below. Need help where marked in bold.

df= pd.read_csv('Info.csv',index_col=False)
App_=df[df['Names']=='Rony']
df_address=[]
address=App['Address']
df_address.append(address)

# This is printing result with index, I do not want to see index .Can index be avoided to print or remove in df_address ?

print(df_address)
dct=next(os.walk('.'))[1] 

#Below is not working , Need help to fix this.How it can work with pandas.Need to check if directories name are available in df_address

for i in df_address:        
    if i in dct:
        #Do Some operation
2
  • You said, "Below is not working". Do you get any errors? Commented May 30, 2018 at 18:20
  • Programs\Python\Python36-32\lib\site-packages\pandas\core\generic.py", line 1121, in __nonzero__ .format(self.__class__.__name__)) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). Commented May 30, 2018 at 18:22

1 Answer 1

1

You do not need to append to a new list explicitly. There is functionality within pandas to convert a series to a list directly:

df_address = App['Address'].tolist()

Moreover, you can chain operations to make your logic more readable. For example, you can filter by row and column simultaneously:

df_address = df.loc[df['Names'] == 'Rony', 'Address']\
               .tolist()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for letting me about tolist() it fixed the entire issue :)
os.walk - how it can be done recursively for directory? I have to write this but it seems long - for i in directories: print(i) l=next(os.walk(i))[1] for k in l: print(k)
I'm not sure. You may need to ask a separate question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.