简体   繁体   English

如何根据同一行中另一列中的值向前填充列值

[英]how to forward fill a column values based on the value in another column in same row

I want to forward fill the amount column based on times column.我想根据时间列向前填充金额列。 for example first value is 2800000.0 , i want this value to be filled 6 times.例如第一个值是 2800000.0 ,我希望这个值被填充 6 次。

amount      times       
2800000.0    6
nan     0    0   
nan     0    0   
nan     0    0   
nan     0    0   
nan     0    0   
nan     0    0   
4750000.0    4         
nan     0    0   
nan     0    0   
nan     0    0   
nan     0    0   
nan     0    0   
nan     0    0    

Desired output:期望的输出:

amount      times       
2800000.0    6
2800000.0    0   
2800000.0    0   
2800000.0    0   
2800000.0    0   
2800000.0    0   
2800000.0    0   
4750000.0    4         
4750000.0    0   
4750000.0    0   
4750000.0    0   
4750000.0    0   
nan     0    0   
nan     0    0   

First create groups by test non missing values with cumulative sum and pass to GroupBy.apply with lambda function with Series.ffill with limit by first value of times per groups:首先,创建通过测试非组缺少与累加和值,并传递给GroupBy.apply与lambda函数与Series.ffill与由第一值限制times每基团:

#if necessary convert strings t onumeric and NaNs
#df['amount'] = pd.to_numeric(df['amount'], errors='coerce')

print (df['amount'].dtype)
float64

g = df['amount'].notna().cumsum()

f = lambda x: x['amount'].ffill(limit=x['times'].iat[0])
df['amount'] = df.groupby(g, group_keys=False).apply(f)
print (df)
       amount  times
0   2800000.0      6
1   2800000.0      0
2   2800000.0      0
3   2800000.0      0
4   2800000.0      0
5   2800000.0      0
6   2800000.0      0
7   4750000.0      4
8   4750000.0      0
9   4750000.0      0
10  4750000.0      0
11  4750000.0      0
12        NaN      0
13        NaN      0

暂无
暂无

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

相关问题 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 根据同一行中另一列的值填充缺失值 - Fill missing value based on value from another column in the same row 如何向前填充 dataframe 列,其中填充的行数限制基于另一列中单元格的值? - How can I forward fill a dataframe column where the limit of rows filled is based on the value of a cell in another column? 如何根据另一列值填充空索引或空行? - How to fill empty index or empty row based on another column value? 如何根据另一列填充 nan 值 - how to fill nan value based on another column 如何用另一列的总和与同一列的前一个值填充一列? - How to fill a column with the sum of another column and the previous value of the same column? 如何根据列值填充所有值? - How to fill all values based on column value? 如何根据另一列填充列的第一行中的NA值 - How to fill NA values in first row of the column on basis of another column 如何用同一列中的值填充 null 列中的 Pyspark Dataframe 值,其在另一列中的对应值相同 - How to fill null values in a Pyspark Dataframe column with values from the same column, whose corresponding value in another column is same 如何在基于另一列的列中填充缺失值 - How to fill an missing values in a column based on another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM