Reputation: 23
I would load one column of a two-column DataFrame with the previous value. Another column with a value of 0.
Original
A B
T
1 10 11
3 20 22
5 15 18
Result
A B
T
1 10 11
2 10 0
3 20 22
4 20 0
5 15 18
How can I do this in pandas dataframe ?
Upvotes: 1
Views: 20
Reputation: 195428
You can do:
mn = df.index.min()
mx = df.index.max()
df = df.reindex(np.arange(mn, mx + 1))
df["A"] = df["A"].ffill()
df["B"] = df["B"].fillna(0)
print(df)
Prints:
A B
T
1 10.0 11.0
2 10.0 0.0
3 20.0 22.0
4 20.0 0.0
5 15.0 18.0
If you want integers, then the last step:
print(df.astype(int))
Prints:
A B
T
1 10 11
2 10 0
3 20 22
4 20 0
5 15 18
Upvotes: 1