0

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())
6
  • Your method doesn't assign the result to the selected columns. It returns a new data frame. If you want to assign them to new columns, df[new_columns] = df[to_lower].apply(lambda x: x.astype(str).str.lower())? Commented Jun 23, 2017 at 3:30
  • @Psidom yes that's what i meant, edited question Commented Jun 23, 2017 at 3:32
  • If you don't want to change the to_lower columns, just provide new column names as new_columns. Commented Jun 23, 2017 at 3:34
  • Example input and output data? Commented Jun 23, 2017 at 3:36
  • @Psidom My code above does what I want, but I want a way to do it as a method on df, so I don't have to assign to df[lower], not to avoid overwriting those columns, but because I want to do it as method chaining on df rather than on a subset of it. Commented Jun 23, 2017 at 3:37

2 Answers 2

1

You can use assign method and unpack the result as keyword argument:

df = pd.DataFrame({'a': ['A', 'a'], 'b': ['B', 'b'], 'c': ['C', 'c']})
to_lower = ['a', 'b']

df.assign(**df[to_lower].apply(lambda x: x.astype(str).str.lower()))

#   a   b   c
#0  a   b   C
#1  a   b   c
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to do this without using the df name inside assign, so I can do it in the middle of a chain of methods?
0

You want this:

for column in to_lower:
    df[column] = df[column].str.lower()

This is far more efficient assuming you have more rows than columns.

1 Comment

Is it possible to do this returning a new dataframe rather than mutating the original?

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.