简体   繁体   中英

How can I change the values in a dataframe column based off the index of a list?

lets say I have a data frame:

x     y     
1     3
2     0
4     1
7     2

and I have a list:

[1,2,7,5]

can I change the values of the Y column based on the value of the index of the list?

for instance, for value 2 of the y column its 0. is it anyway to take that 0 and look at the 0th index value of the list and changes it to that value so the 4 would become 1 in the y column?

and for the rest of the values?

so it would be:

x     y     
1     5
2     1
4     2
7     7

thanks guys

EDIT: FIXED INDEXING (started from 1 instead of 0)

Just do with

df.y=np.array(l)[df.y-1]# here i subtract 1 since the index from pandas or numpy is from 0 by default 
df
Out[52]: 
   x  y
0  1  7
1  2  5
2  4  1
3  7  2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM