繁体   English   中英

Pandas 数据框替换值

[英]Pandas Dataframe replace value

我有一个像下面这样的输入,其中列名在数据框中重复..

1. Product  Month  lives  paid
2. A        June    123   12
3.          sep    13    13
4.          aug    113   14
5. Product Month  lives  paid
6. B       July    1123   14
7.         July    13     14
8.         Sep     1123   14
9. Product Month  lives  paid
10. C      Mar    12      11
11.        Apr    13      13

因此,我希望将第一个产品名称 A 复制下来,直到看到 B 开头的新“产品列”名称,依此类推。

我正在尝试这样的事情,但这个不考虑产品词:

if data['Product'].isnull().any():
   data['Product'] = data['Product'].replace(np.nan,data['Product'][0])

输出需要以下任何一项:

1. Product  Month  lives  paid
2. A        June    123   12
3. A         sep    13    13
4. A         aug    113   14
5. Product Month  lives  paid
6. B       July    1123   14
7. B       July    13    14
8. B       Sep     1123  14
9. Product Month  lives  paid
10. C      Mar    12      11
11. C      Apr    13     13

或者

1. Product  Month  lives  paid
2. A        June    123   12
3. A         sep    13    13
4. A         aug    113   14
5. B       July    1123   14
6. B        July    13    14
7. B        Sep     1123  14
8. C        Mar    12      11
9. C        Apr    13     13

可以对代码提出任何建议吗?...

正如亚图在评论中回答的那样:

data['Product'] = data['Product'].ffill(axis = 0)

暂无
暂无

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

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