如何将大 pandas DataFrame值复制到0以填补? [英] How to copy pandas DataFrame values down to fill 0's?

查看:95
本文介绍了如何将大 pandas DataFrame值复制到0以填补?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上,如何将DataFrame A转换为DataFrame B?

So basically, how do I convert DataFrame A to DataFrame B?

A:

Index Value
01 42
02 0
03 0
04 57
05 0
... ...

B:

Index Value
01 42
02 42
03 42
04 57
05 57
... ...


推荐答案

您将需要转换为浮动(如有必要,不会工作为ints)为此工作查看文档 替换 ,特别是这里使用的方法是 ffill 这意味着正向填充:

You will need to convert to floats (if necessary, it won't work for ints) for this to work see the docs on replace and in particular the method used here is ffill which means forward fill:

df = pd.DataFrame({'value':[42,0,0,57,0]})
df
Out[16]:

   value
0     42
1      0
2      0
3     57
4      0

In [17]:
df.value = df.value.astype(float)
df.replace(to_replace=0, method='ffill')
Out[17]:

   value
0     42
1     42
2     42
3     57
4     57

您可以随时转换回来:

df.value = df.value.astype(int)

这篇关于如何将大 pandas DataFrame值复制到0以填补?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆