#๐ pandas dataframe manipulation
68 messages ยท Page 1 of 1 (latest)
@short bronze
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
price side size num_orders exchange depth_level
0 25.30000000 bid 475.34900000 16 KuCoin 210
1 25.20000000 bid 5118.40670000 53 KuCoin 209
...
205 0.30000000 bid 10.00000000 1 KuCoin 5
206 0.20000000 bid 28.95730000 2 KuCoin 4
207 0.10000000 bid 272.25350000 7 KuCoin 3
208 0.00000000 bid 278964.05160000 14 KuCoin 2
209 25.30000000 ask 5503.47890000 46 KuCoin 1
210 25.40000000 ask 2269.94370000 29 KuCoin 2
whats the issue
i have a dataframe in a variable in that format, its a little hard to see, but right now index 0 it is showing highest bid price and then descending to the midpoint. but i want midpoint where depth level is 1 to have that first number 25.3, and depth 2 to be subsequently descending, but right now its doing that starting from index 0 until the midpoint, rather than from midpoint to index 0 for the bids
to be clear its not that i want index to change, index should just be
1
2 3 4 5, and so on
are you trying to sort it by index or am i not understanding?
this is the index?
ah i see
sorry but i havent used pandas in a long while so im not certain how you would do it
first two rows, in case helpful, to also show headers. nw, in case anyone out there knows
Hey @short bronze So from what I gather, you are want to reverse the order of this but keep the indices the same?
but only reverse the order for when side = 'bid'
ahh ok
This should do the trick for you:
bid_df = df[df['side'] == 'bid'].iloc[::-1]
ask_df = df[df['side'] == 'ask']
fixed_df = pd.concat([bid_df, ask_df]).sort_index()
fixed_df.reset_index(drop=True, inplace=True)
Are you displaying the new df?
Thanks! Let me see what I did wrong here.
@short bronze try removing the .sort_index()
That re-sorted it back into the wrong order again haha ๐
What exactly are you trying to achieve?
i've been restructuring this dataframe for the past hour
Maybe I'm misunderstanding
starting from midpoint, a market orderbook in the shape of a dataframe that goes outward on both sides
depth will be 1 for first bid, 1 for first ask at the very middle, and then 2 and 2, 3 and 3
until its assymetric, where lets say i have more bids than asks
then the depth will go on
and in terms of price, bids and asks will be closest at the midpoint and farther and farther away
exchange won't change, and num_orders is just the number of trades at that price point
which is mostly gonna be one, but i want to make sure that that's also correctly captured, because later on i might aggregate price into bundles of every $0.01 instead of having it fully spread out like current dataframe
Ahh, I think I get it:
bids = df[df['side'] == 'bid'].sort_values('depth_level')
asks = df[df['side'] == 'ask'].sort_values('depth_level')
fixed_df = pd.concat([bids, asks])
fixed_df.reset_index(drop=True, inplace=True)
grab all the bids, sort by depth
grab all the asks, do the same
put the two together
reindex
oh, you want it to start at 1s in the middle
easy fix
bids = df[df['side'] == 'bid'].sort_values('depth_level', ascending=False)
asks = df[df['side'] == 'ask'].sort_values('depth_level')
fixed_df = pd.concat([bids, asks])
fixed_df.reset_index(drop=True, inplace=True)
this reverts price back to counting from 0/lowest bid though though, when we want it to start from highest bid, closest to the lowest ask
I thought we were sorting by depth level, not price. Were you wanting to change the depth values?
no, maintain everything except flip the price
starting from the midpoint for bids only
rn it starts from 0
for bids
A bit more complex but maybe this one:
bids = df[df['side'] == 'bid'].sort_values('price', ascending=False)
asks = df[df['side'] == 'ask'].sort_values('price')
bids['depth_level'] = range(1, len(bids) + 1)
asks['depth_level'] = range(1, len(asks) + 1)
bids = bids.sort_values('depth_level', ascending=False)
fixed_df = pd.concat([bids, asks])
fixed_df.reset_index(drop=True, inplace=True)
ahh, that did it! would i still need
midpoint_index = len(df[df['side'] == 'bid'])
df['depth_level'] = df.apply(lambda x: abs(midpoint_index - x.name) + 1, axis=1)
no, I don't think so
I already recreated the depth_level column after sorting and don't need the midpoint
could you explain it a bit
# sort the bids by price in descending order
bids = df[df['side'] == 'bid'].sort_values('price', ascending=False)
# sort the asks by price in ascending order
asks = df[df['side'] == 'ask'].sort_values('price')
# recreate the depth_level values for bids and asks from 1 - length
bids['depth_level'] = range(1, len(bids) + 1)
asks['depth_level'] = range(1, len(asks) + 1)
# reverse the bids so they are descending by depth
bids = bids.sort_values('depth_level', ascending=False)
# smash them together
fixed_df = pd.concat([bids, asks])
# reindex
fixed_df.reset_index(drop=True, inplace=True)
Any time! Cheers!
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.