美文网首页
pandas高效遍历总结

pandas高效遍历总结

作者: _龙雀 | 来源:发表于2019-06-12 22:51 被阅读0次

只需要取值,不需要直接修改df:

  • 遍历行
    • iterrows
for index,row in df.iterrows():
    index为df的index
    row['列名']
for row in df.itertuples():
    取值方法:
    row[0]为index
    row[1]为第一列 依次类推

需要在遍历的同时修改值

  • apply

单独修改某列的值:

  df['score'] = df['score'].apply(lambda x: 1 if x>=flag else 0)
  #大于flag修改为1,小于修改为0

综合多个列修改值

df['score'] = df.apply(lambda x: x['a']+x['b'],axis=1)

相关文章

网友评论

      本文标题:pandas高效遍历总结

      本文链接:https://www.haomeiwen.com/subject/fnfdfctx.html