简体   繁体   English

如何移动单个 dataframe 行上的列值 (Python Pandas)

[英]How to shift column values on a single dataframe row (Python Pandas)

The database I'm working with looks something like this:我正在使用的数据库看起来像这样:

         C1        C2         C3

R1      Nan       Nan         2

R2      1         Nan        Nan

R3      2         2          Nan

and I wanted to copy and shift that last value in each cell to the others in its row, and have something like this:我想将每个单元格中的最后一个值复制并移动到其行中的其他值,并且有这样的东西:

         C1        C2         C3

R1       2         2          2

R2      1          1          1

R3      2         2           2

How can I do it?我该怎么做? I'm new with Pandas and I would like some help我是 Pandas 的新用户,我需要一些帮助

You can try to ffill / bfill the rows:您可以尝试ffill / bfill行:

out = df.ffill(axis=1).bfill(axis=1, downcast='infer')

output: output:

    C1  C2  C3
R1   2   2   2
R2   1   1   1
R3   2   2   2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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