熊猫 python groupby 取决于其他列表

时间:2021-03-23 20:23:55

标签: python pandas

我有 x 辆和 y 辆汽车,想添加一个包含汽车块 (1-3) 的新列。 “y_block_number_ends”给出了块结束的 y 坐标。

import pandas as pd

x_start = 1
y_start = 1
size_city = 5

y_block_number_ends = [2,4]

cars = pd.DataFrame({'x': np.repeat(np.arange(x_start,x_start+size_city),size_city),
                    'y': np.tile(np.arange(y_start,y_start+size_city),size_city)})

#cars['block_y']= cars.where([cars['y'] > 0 | (cars['y'] <= 2)])

print(cars)

新的数据框看起来像这样

    x  y    block
0   1  1      1
1   1  2      1
2   1  3      2
3   1  4      2
4   1  5      3
5   2  1      1
6   2  2      1
7   2  3      2
8   2  4      2
9   2  5      3
10  3  1      1
11  3  2      1
12  3  3      2
13  3  4      2
14  3  5      3
15  4  1      1
16  4  2      1
17  4  3      2
18  4  4      2
19  4  5      3
20  5  1      1
21  5  2      1
22  5  3      2
23  5  4      2
24  5  5      3

1 个答案:

答案 0 :(得分:5)

from discord_slash import cog_ext class Slash(commands.Cog): def __init__(self, bot): self.bot = bot @cog_ext.cog_slash(name="test") async def _test(self, ctx: SlashContext, a=None, b=None): embed = discord.Embed(title="embed test") await ctx.send(content="test", embeds=[embed])

pd.cut

cars.assign(block=pd.cut(cars.y, [0, 2, 4, float('inf')], labels=[1, 2, 3])) x y block 0 1 1 1 1 1 2 1 2 1 3 2 3 1 4 2 4 1 5 3 5 2 1 1 6 2 2 1 7 2 3 2 8 2 4 2 9 2 5 3 10 3 1 1 11 3 2 1 12 3 3 2 13 3 4 2 14 3 5 3 15 4 1 1 16 4 2 1 17 4 3 2 18 4 4 2 19 4 5 3 20 5 1 1 21 5 2 1 22 5 3 2 23 5 4 2 24 5 5 3

searchsorted