Can somebody explain how this works? df.columns = list(map(str, df.columns))
CodePudding user response:
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.