Reputation: 115
I have dataframe like this:
int_time | leng | id |
---|---|---|
3 | 4123 | 1 |
5 | 243 | 2 |
7 | 1232 | 3 |
8 | 3883 | 4 |
.. | ... | .. |
and I want to sort values in int_time and leng based on value id. So output will look like this:
int_time | leng | id |
---|---|---|
0 | 0 | 1 |
0 | 0 | 2 |
3 | 4123 | 3 |
4 | 0 | 4 |
5 | 243 | 5 |
6 | 0 | 6 |
7 | 1232 | 7 |
8 | 3883 | 8 |
.. | ... | .. |
In other words, i want to change row index for int_time and leng based on value in id. Can somebody help me with this please?
Upvotes: 0
Views: 185
Reputation: 14949
you can use merge -
n = df['int_time'].max()
new_df = pd.DataFrame({'id': range(1, int(n) + 1)})
new_df = new_df.merge(df, left_on='id', right_on='int_time', how= 'left').fillna(0).drop('id_y', axis=1).rename(columns={'id_x': 'Id'})
print(new_df)
output-
Id | int_time | leng |
---|---|---|
1 | 0.0 | 0.0 |
2 | 0.0 | 0.0 |
3 | 3.0 | 4123.0 |
4 | 0.0 | 0.0 |
5 | 5.0 | 243.0 |
6 | 0.0 | 0.0 |
7 | 7.0 | 1232.0 |
8 | 8.0 | 3883.0 |
Upvotes: 1