Good morning.
I've a little problem and I'm sure that it has an easy solution, but I can't find it.
I've a dataframe like this:
df =
num unit
0 10 minutes
1 20 seconds
2 30 newtons
3 10 seconds
And, for reasons that doesn't matter, I want to rewrite the num
column adding it corresponding unit, getting the next result:
df =
num unit
0 '10 minutes' minutes
1 '20 seconds' seconds
2 '30 newtons' newtons
3 '10 seconds' seconds
I'm using the following code:
df.num = df.num.apply(lambda x: '{n} {u}'.format(n = x, u = unidecode(df.loc[df[df.num == x].index.values[0], 'unit'])))
The problem comes when there are 2 or more equal values in the first column, like in rows 0 and 3, so my wrong final result, due to ...values[0]
, is:
df =
num unit
0 '10 minutes' minutes
1 '20 seconds' seconds
2 '30 newtons' newtons
3 '10 minutes' seconds
Thanks you very much in advance.