英文:
How to add values into columns in pandas
问题
如何使用pandas数据框架将列X和Y的值添加到X2和Y2中?例如,我想将Brad的X和Y值添加到Anton的X2和Y2列中,将Mikel添加到Brad的X2和Y2列中,以此类推,直到结束。
英文:
How can I add the values from column X and Y to X2 and Y2 using pandas dataframework? For example I want the X and Y value of Brad to be added into the X2 and Y2 column of Anton and for Mikel to be added to X2 and Y2 of Brad and so on till the end.
答案1
得分: 0
你正在寻找Series.shift()
:
df['X2'] = df['X'].shift(-1)
df['Y2'] = df['Y'].shift(-1)
请注意,最后一行将为NaN
:
Player X Y X2 Y2
0 Anton 49.5 50.5 36.4 44.5
1 Brad 36.4 44.5 20.3 30.7
2 Mikel 20.3 30.7 10.0 44.4
3 Jimmy 10.0 44.4 NaN NaN
英文:
You are looking for Series.shift()
:
df['X2'] = df['X'].shift(-1)
df['Y2'] = df['Y'].shift(-1)
Note that the last row will be NaN
:
Player X Y X2 Y2
0 Anton 49.5 50.5 36.4 44.5
1 Brad 36.4 44.5 20.3 30.7
2 Mikel 20.3 30.7 10.0 44.4
3 Jimmy 10.0 44.4 NaN NaN
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论