python - Pandas - 如何创建一个新列,该新列从前一行或下一行(如果第一行)的列中获取值

标签 python pandas dataframe

给定如下所示的数据

Time    Col01   Col02
05:17:55.703000 NaN NaN
05:17:55.703000 891 12
05:17:55.703000 891 13
05:17:55.703000 891 15
05:17:55.703000 891 16
05:17:55.703000 891 17
05:17:55.703000 891 18
05:17:55.707000 892  0
05:17:55.707000 892  1
05:17:55.707000 892  5
05:17:55.707000 892  6
05:17:55.707000 892  7
05:17:55.708000 NaN  NaN
05:17:55.711000 892 10
05:17:55.711000 892 11
05:17:55.711000 892 12
05:17:55.723000 893 11
05:17:55.723000 893 15
05:17:55.723000 893 16
05:17:55.726000 NaN  NaN

需要创建两个新的列,如果当前列为 NaN,则根据以下逻辑,它们将起作用

+-----------------+-------+-------+----------+----------+----------------------------------------+
|      Time       | Col01 | Col02 | Col01new | Col02new |                                        |
+-----------------+-------+-------+----------+----------+----------------------------------------+
| 05:17:55.703000 | NaN   | NaN   |      891 |       12 | if NaN & first row, fill from next row |
| 05:17:55.703000 | 891   | 12    |      891 |       12 |                                        |
| 05:17:55.703000 | 891   | 13    |      891 |       13 |                                        |
| 05:17:55.703000 | 891   | 15    |      891 |       15 |                                        |
| 05:17:55.703000 | 891   | 16    |      891 |       16 |                                        |
| 05:17:55.703000 | 891   | 17    |      891 |       17 |                                        |
| 05:17:55.703000 | 891   | 18    |      891 |       18 |                                        |
| 05:17:55.707000 | 892   |  0    |      892 |        0 |                                        |
| 05:17:55.707000 | 892   |  1    |      892 |        1 |                                        |
| 05:17:55.707000 | 892   |  5    |      892 |        5 |                                        |
| 05:17:55.707000 | 892   |  6    |      892 |        6 |                                        |
| 05:17:55.707000 | 892   |  7    |      892 |        7 |                                        |
| 05:17:55.708000 | NaN   |  NaN  |      892 |        7 | if NaN fill from previous row          |
| 05:17:55.711000 | 892   | 10    |      892 |       10 |                                        |
| 05:17:55.711000 | 892   | 11    |      892 |       11 |                                        |
| 05:17:55.711000 | 892   | 12    |      892 |       12 |                                        |
| 05:17:55.723000 | 893   | 11    |      893 |       11 |                                        |
| 05:17:55.723000 | 893   | 15    |      893 |       15 |                                        |
| 05:17:55.723000 | 893   | 16    |      893 |       16 |                                        |
| 05:17:55.726000 | NaN   |  NaN  |      893 |       16 | if NaN fill from previous row          |
+-----------------+-------+-------+----------+----------+----------------------------------------+

最佳答案

按照正确的顺序填写,先向前,然后向后(如果为空,则仅获取第一行)。

pd.concat([df, df[['Col01', 'Col02']].ffill().bfill(downcast='infer').add_suffix('new')], axis=1)
<小时/>
               Time  Col01  Col02  Col01new  Col02new
0   05:17:55.703000    NaN    NaN       891        12
1   05:17:55.703000  891.0   12.0       891        12
2   05:17:55.703000  891.0   13.0       891        13
3   05:17:55.703000  891.0   15.0       891        15
4   05:17:55.703000  891.0   16.0       891        16
5   05:17:55.703000  891.0   17.0       891        17
6   05:17:55.703000  891.0   18.0       891        18
7   05:17:55.707000  892.0    0.0       892         0
8   05:17:55.707000  892.0    1.0       892         1
9   05:17:55.707000  892.0    5.0       892         5
10  05:17:55.707000  892.0    6.0       892         6
11  05:17:55.707000  892.0    7.0       892         7
12  05:17:55.708000    NaN    NaN       892         7
13  05:17:55.711000  892.0   10.0       892        10
14  05:17:55.711000  892.0   11.0       892        11
15  05:17:55.711000  892.0   12.0       892        12
16  05:17:55.723000  893.0   11.0       893        11
17  05:17:55.723000  893.0   15.0       893        15
18  05:17:55.723000  893.0   16.0       893        16
19  05:17:55.726000    NaN    NaN       893        16

关于python - Pandas - 如何创建一个新列,该新列从前一行或下一行(如果第一行)的列中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58042869/

相关文章:

python - 如何将带有索引的选定列转换为 Pandas 中的元组列表

python - 如何使用子图创建 Pandas groupby 图

python - PerformanceWarning : dropping on a non-lexsorted multi-index without a level parameter may impact performance. 如何摆脱它?

python - 在 Pandas 中将列拆分为列表

python - 通过分组计算数据帧中值的差异

python - 将 OECD API 中的数据读入 python(和 pandas)

python - 谁能告诉我 Gensim 使用的模型(skipgram/CBOW)?

Python 排序函数不执行任何操作

python - celery 如何终止并重新启动任务

r - 处理data.frame列表时出现意外错误