In Python, there is a built-in method that can change a string that is in uppercase to lowercase. I thought this would work: df = df.apply(lambda x: x.lower() if(isinstance(x, str)) else x)īut it doesn't work. When changing a string to lowercase, it only applies to the letters. Rather than forcing the type to be a string, I'd like to assess whether values are strings and then - if they are - convert them to lowercase, and - if they are not strings - leave them as they are. str.lower() is the built-in method made specifically for that purpose. str accessor with string values, which use np.object_ dtype in pandas Converting strings to lowercase is pretty straightforward to perform in Python. This causes the lower function, if applied like above, to throw an exception: AttributeError: Can only use. It's not as simple as just using: df = df.str.lower()īecause I'm iterating over a lot of dataframes, and some of them (but not all) have both strings and integers in the column of interest.
0 Comments
Leave a Reply. |