0

Can somebody explain how this works? df.columns = list(map(str, df.columns))

1 Answer 1

2

Your code is not the best way to convert column names to string, use instead:

df.columns = df.columns.astype(str)

Your code:

df.columns = list(map(str, df.columns))

is equivalent to:

df.columns = [str(col) for col in df.columns]

map: for each item in df.columns (iterable), apply the function str on it but the map function returns an iterator, so you need to explicitly execute list to generate the list.

Sign up to request clarification or add additional context in comments.

Comments

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.