I have below data frame
import pandas as pd, numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('AB'))
>>> df
A B
0 50 59
1 7 15
2 35 54
3 86 62
4 75 68
.. .. ..
95 16 32
96 2 83
97 65 18
98 91 39
99 33 50
Now let say I want to replace values of the first column using assign
method. So I can write
df.assign(A = lambda x : x['A'] * 2)
But instead of calling column by name, I want to use index, i.e. something like
df.assign(:0 = lambda x : x.iloc[:, 0] * 2)
Above code is generating error. Is there any way to achieve this?
CodePudding user response:
You can store the index of your column you want to update (assuming i
as the index and 0
as the location), and use assign
as follows:
i = 0
colname = df.columns[i]
res = df.assign(**{colname : lambda x : x[colname] * 2})
prints:
>>> print(df)
A B
0 38 32
1 19 17
2 38 69
3 60 4
4 26 81
.. .. ..
95 46 46
96 87 44
97 43 42
98 18 77
99 25 74
[100 rows x 2 columns]
>>> print(res)
A B
0 76 32
1 38 17
2 76 69
3 120 4
4 52 81
.. ... ..
95 92 46
96 174 44
97 86 42
98 36 77
99 50 74
[100 rows x 2 columns]
CodePudding user response:
Why don't you use the following:
df.iloc[:,1] = df.iloc[:,1] * 2
this multiplies all the values in the 2nd column by 2, as requested