Pandas - help changing values

how can I change to Females and Males instead of all of the numbers?

array([‘F’, ‘F.1’, ‘M’, ‘M.1’, ‘F.2’, ‘F.3’, ‘F.4’, ‘M.2’, ‘F.5’, ‘M.3’,
‘F.6’, ‘M.4’, ‘F.7’, ‘M.5’, ‘F.8’, ‘F.9’, ‘F.10’, ‘M.6’, ‘M.7’,
‘M.8’, ‘M.9’, ‘M.10’, ‘F.11’, ‘M.11’, ‘Unnamed: 25’, ‘Unnamed: 26’,
‘M.12’, ‘M.13’, ‘M.14’, ‘M.15’, ‘F.12’, ‘M.16’, ‘M.17’, ‘M.18’,
‘M.19’, ‘M.20’, ‘F.13’, ‘M.21’, ‘M.22’, ‘F.14’, ‘M.23’, ‘M.24’,
‘M.25’, ‘F.15’, ‘F.16’, ‘F.17’, ‘M.26’, ‘M.27’, ‘M.28’, ‘F.18’,
‘M.29’, ‘F.19’, ‘M.30’, ‘F.20’, ‘F.21’, ‘M.31’, ‘M.32’, ‘F.22’,
‘M.33’, ‘M.34’, ‘M.35’, ‘M.36’, ‘M.37’, ‘F.23’, ‘M.38’, ‘M.39’,
‘F.24’, ‘M.40’, ‘M.41’, ‘F.25’, ‘M.42’, ‘F.26’, ‘F.27’, ‘M.43’,
‘M.44’, ‘M.45’, ‘M.46’, ‘F.28’, ‘M.47’, ‘M.48’, ‘F.29’, ‘M.49’,
‘M.50’, ‘M.51’, ‘F.30’, ‘M.52’, ‘M.53’, ‘F.31’, ‘M.54’, ‘M.55’,
‘F.32’, ‘M.56’, ‘M.57’, ‘F.33’, ‘M.58’, ‘M.59’, ‘F.34’, ‘M.60’,
‘M.61’, ‘F.35’, ‘F.36’, ‘F.37’, ‘M.62’, ‘F.38’, ‘F.39’, ‘M.63’,
‘M.64’, ‘M.65’, ‘M.66’, ‘M.67’, ‘M.68’, ‘M.69’, ‘F.40’, ‘M.70’],

if df[‘Sex’].str.startswith(‘F’):

ValueError Traceback (most recent call last)
----> 1 if df[‘Sex’].str.startswith(‘F’):
2 df[‘Sex’]==“Female”

~\anaconda3\lib\site-packages\pandas\core\ in nonzero(self)
1325 def nonzero(self):
-> 1326 raise ValueError(
1327 f"The truth value of a {type(self).name} is ambiguous. "
1328 “Use a.empty, a.bool(), a.item(), a.any() or a.all().”

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Note that == (double equals) is to compare values and = (single equal) is to assign values. Maybe you should replace the == with =. Try it and let me know.

Pandas follows the numpy convention of raising an error when you try to convert something to a bool. This happens in a if or when using the boolean operations, and, or, or not. It is not clear what the result of.


5 == pd.Series([12,2,5,10])

The result you get is a Series of booleans, equal in size to the pd.Series in the right hand side of the expression. So, you get an error. The problem here is that you are comparing a pd.Series with a value, so you’ll have multiple True and multiple False values, as in the case above. This of course is ambiguous, since the condition is neither True or False. You need to further aggregate the result so that a single boolean value results from the operation. For that you’ll have to use either any or all depending on whether you want at least one (any) or all values to satisfy the condition.

(5 == pd.Series([12,2,5,10])).all()
# False


(5 == pd.Series([12,2,5,10])).any()
# True