0

I have a dataframe with multiple blank values and different data types per column. For example I have the following dataframe I want to select the columns "Name", "Country" and "Job" and apply capwords to all this column only if value is ALL CAPSLOCK (can convert all into str before and that is not a problem). So this dataframe:

ID  Name    Country Job
1   PAULO   BRAZil  JOHN'S DRIVER
2   Joao    $   -
3   Maria   np.nan  DriveR
3   MARIA   Portugal    DRIVER
4   PEdro   ARGENTINA   DRIVER

Would return this output:

ID  Name    Country Job
1   Paulo   BRAZil  John's Driver
2   Joao    $   -
3   Maria   np.nan  DriveR
3   Maria   Portugal    Driver
4   PEdro   Argentina   Driver

What would be the best way to do it?

Paulo Cortez
  • 609
  • 4
  • 10

1 Answers1

1

Let's try applymap for each value

import string

cols = ['Name', 'Country', 'Job']

df[cols] = df[cols].applymap(lambda val: string.capwords(val) if val.isupper() else val)
print(df)

   ID   Name    Country            Job
0   1  Paulo     BRAZil  John's Driver
1   2   Joao          $              -
2   3  Maria     np.nan         DriveR
3   3  Maria   Portugal         Driver
4   4  PEdro  Argentina         Driver
5   5  PEdro  Argentina         Driver
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • Hello, thanks for that! Is there a way I could use the same solution but instead of title case to use "capwords"? I believe it would be better to handle names with apostrophe. – Paulo Cortez Aug 25 '22 at 07:03
  • @PauloCortez Show an example? – Ynjxsjmh Aug 25 '22 at 07:05
  • Please refer to this topic: https://stackoverflow.com/questions/8199966/python-title-with-apostrophes I added an example to the data. Thanks! – Paulo Cortez Aug 25 '22 at 10:00
  • Hi. sorry, I just checked and the function transforms the blank values into "nan" into string format. Can it returns the np.nan as described. – Paulo Cortez Aug 26 '22 at 07:13
  • @PauloCortez Don't understand, give an example. – Ynjxsjmh Aug 26 '22 at 08:02
  • In the same example I gave, 3rd row for me where country was "np.nan" it returns "nan" in string format – Paulo Cortez Aug 26 '22 at 08:28
  • @PauloCortez Don't understand what `np.nan` is, is this a literal string or a type? In any case, you can special handle it in the lambda function. The lambda function also can be extended to a user defined function. – Ynjxsjmh Aug 26 '22 at 09:36
  • applymap has an argument "na_action = 'ignore" to handle NaN cases as you can see here: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.applymap.html so I believe that would work – Paulo Cortez Aug 26 '22 at 11:19