Pandas 熊猫用最后一个已知值填充空的尾随值

Pandas 熊猫用最后一个已知值填充空的尾随值,pandas,Pandas,我有几个不同结束时间段的专栏 需要用最后一个已知值填充空数据 有没有一种方法可以做到这一点,而不必在结束日期上循环基数 我需要过去4个月的增益总和等于-57129.0 gain_sum_x gain_sum_y month 2014-09-30 -97747 -41355.0 2014-10-31 -112928 -47394.0 2014-11-30 -131638 -571

我有几个不同结束时间段的专栏

需要用最后一个已知值填充空数据

有没有一种方法可以做到这一点,而不必在结束日期上循环基数

我需要过去4个月的增益总和等于-57129.0

        gain_sum_x  gain_sum_y
month                             
2014-09-30      -97747    -41355.0
2014-10-31     -112928    -47394.0
2014-11-30     -131638    -57129.0
2014-12-31     -161370         0.0
2015-01-31     -168832         0.0
2015-02-28     -151930         0.0
2015-03-31     -162077         0.0
谢谢。

如果要用最后一个非0值替换所有0值,我认为您需要使用ffill with method='ffill':

如果要指定“替换”列,谢谢:

具有多个0的示例:

但如果需要仅替换最后0,则需要将最后0替换为NaN:

如果要用最后一个非0值替换所有0值,我认为需要使用ffill with method='ffill'

如果要指定“替换”列,谢谢:

具有多个0的示例:

但如果需要仅替换最后0,则需要将最后0替换为NaN:


以下是我要做的工作:

# loop through all the columns
for i, column in enumerate ( df.columns ):

    # find the last row with value not zero
    x = df [ df[column] != 0 ].index [ -1 ]
    # get the last value before the zero values
    y = df[column] [x]
    # find and fill the rows greater than date "x" with value "y"
    mask = (df.index > x)
    df[column] [ mask ] = y

我希望这是好熊猫。感谢大家。

以下是我的工作:

# loop through all the columns
for i, column in enumerate ( df.columns ):

    # find the last row with value not zero
    x = df [ df[column] != 0 ].index [ -1 ]
    # get the last value before the zero values
    y = df[column] [x]
    # find and fill the rows greater than date "x" with value "y"
    mask = (df.index > x)
    df[column] [ mask ] = y
我希望这是好熊猫。感谢大家。

试试这个:

df.fillnamethod='pad' 具有多个具有不同结束时间段的列

需要用最后一个已知值填充空数据

有没有一种方法可以做到这一点,而不必在结束日期上循环基数

我需要过去4个月的增益总和等于-57129.0

        gain_sum_x  gain_sum_y
month                             
2014-09-30      -97747    -41355.0
2014-10-31     -112928    -47394.0
2014-11-30     -131638    -57129.0
2014-12-31     -161370         0.0
2015-01-31     -168832         0.0
2015-02-28     -151930         0.0
2015-03-31     -162077         0.0
月|收益|总和| x |收益|总和| y -|-|- 2014-09-30| -97747| -41355.0 2014-10-31| -112928| -47394.0 2014-11-30| -131638| -57129.0 2014-12-31| -161370| 0.0 2015-01-31| -168832| 0.0 2015-02-28| -151930| 0.0 2015-03-31| -162077| 0.0 谢谢。

试试这个:

df.fillnamethod='pad' 具有多个具有不同结束时间段的列

需要用最后一个已知值填充空数据

有没有一种方法可以做到这一点,而不必在结束日期上循环基数

我需要过去4个月的增益总和等于-57129.0

        gain_sum_x  gain_sum_y
month                             
2014-09-30      -97747    -41355.0
2014-10-31     -112928    -47394.0
2014-11-30     -131638    -57129.0
2014-12-31     -161370         0.0
2015-01-31     -168832         0.0
2015-02-28     -151930         0.0
2015-03-31     -162077         0.0
月|收益|总和| x |收益|总和| y -|-|- 2014-09-30| -97747| -41355.0 2014-10-31| -112928| -47394.0 2014-11-30| -131638| -57129.0 2014-12-31| -161370| 0.0 2015-01-31| -168832| 0.0 2015-02-28| -151930| 0.0 2015-03-31| -162077| 0.0
谢谢。

对于特定的列,只执行df.replace{'gain\u sum\u y':{0:np.nan}。如果我不好,我没有指出在数据开始之前可能存在合法的0值。处理多个时间序列。所以我只需要更新最后一个发布值之后的结束值。好的,然后使用第二个解决方案。对于特定列,只执行df.replace{'gain\u sum\u y':{0:np.nan}。ffillmy bad,我没有指出在数据开始之前可能存在合法的0值。处理多个时间序列。所以我只需要更新最后一个发布值之后的结束值。好,然后使用第二个解决方案。
import pandas as pd
import datetime

df = pd.read_csv("data.txt")
df['month'] = pd.to_datetime(df['month'])
mask = df['month'] > datetime.datetime.strptime("2014-12-01",'%Y-%m-%d')
df['gain_sum_y'][mask] = -57129.0 
df


month   gain_sum_x  gain_sum_y
0   2014-09-30  -97747  -41355.0
1   2014-10-31  -112928 -47394.0
2   2014-11-30  -131638 -57129.0
3   2014-12-31  -161370 -57129.0
4   2015-01-31  -168832 -57129.0
5   2015-02-28  -151930 -57129.0
6   2015-03-31  -162077 -57129.0
# loop through all the columns
for i, column in enumerate ( df.columns ):

    # find the last row with value not zero
    x = df [ df[column] != 0 ].index [ -1 ]
    # get the last value before the zero values
    y = df[column] [x]
    # find and fill the rows greater than date "x" with value "y"
    mask = (df.index > x)
    df[column] [ mask ] = y