I have a dataframe containing strings and NaNs. I want to str.lower() certain columns by name to_lower = ['b', 'd', 'e']. Ideally I could do it with a method on the whole dataframe, rather than with a method on df[to_lower]. I have
df[to_lower] = df[to_lower].apply(lambda x: x.astype(str).str.lower())
but I would like a way to do it without assigning to the selected columns.
df = pd.DataFrame({'a': ['A', 'a'], 'b': ['B', 'b']})
to_lower = ['a']
df2 = df.copy()
df2[to_lower] = df2[to_lower].apply(lambda x: x.astype(str).str.lower())
df[new_columns] = df[to_lower].apply(lambda x: x.astype(str).str.lower())?to_lowercolumns, just provide new column names asnew_columns.df, so I don't have to assign todf[lower], not to avoid overwriting those columns, but because I want to do it as method chaining ondfrather than on a subset of it.