I have a dataframe as follows. What I would like is to generate another column (freq
) where the rows will have values according to this logic:
If Mode column value starts with a digit
m
, then fill-in digitn
in the freq column.- m: 1, n: 12 - m: 6, n: 4 - m: 7, n: 2 - m: 8, n: 1
DataFrame
Mode
0 602
1 603
2 700
3 100
4 100
5 100
6 802
7 100
8 100
9 100
10 100
Here is the logic that I tried implementing. But somehow it does not seem to work. Even if you could suggest some alternate solution, without using my code, that will work as well.
def check_mode(Mode):
freq = ''
if (Mode.str.startswith('8')).any():
freq = 1
elif (Mode.startswith("7")).all():
freq = 2
elif (Mode.startswith("6")).any():
freq = 4
elif (Mode.startswith("1")).any():
freq = 12
return freq
df['freq']=check_mode(df_ia['Mode'].values)
Some observations
if I use:
if (Mode.str.startswith('8')).any():
I receive error:
AttributeError: 'numpy.ndarray' object has no attribute 'str'
if I use:
if (Mode.startswith('8')).any():
I receive:
AttributeError: 'numpy.ndarray' object has no attribute 'startswith'
Any help will be much appreciated. Thank you.