I have an example pandas data frame (df) with the following values:
Name | Label |
---|---|
Cat | A12 |
Dog | 67 |
Mouse | 20 |
Monkey | 10.8 |
Parrot | V20.6 |
I need to update the value of the label column, which has a mix of numerical labels and integer labels but is stores as a string, to have a leading zero in certain circumstances.
What I need is where the length of the label is less than 3, there needs to be a leading zero, and where the length of the digits to the left of the decimal is less than 3, to also have this leading zero.
So, the table output I need is:
Name | Label |
---|---|
Cat | A12 |
Dog | 067 |
Mouse | 020 |
Monkey | 010.8 |
Parrot | V20.6 |
I have tried a few different df.loc iterations for the first criteria, where length is equal to 3 but I got an error I have never gotten before and cannot find much information on: ‘cannot use a single bool to index into setitem’
And I am not even sure how to approach the second criteria. Is it even possible?
user23237706 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.