Home > Mobile >  Create a list by the index of the column values meet the condition python
Create a list by the index of the column values meet the condition python

Time:04-12

I have a dataframe with two columns ['A', 'B']. The columns are sorted already. I want to find a list of A values based on every first n times of 100 of column B and add min and max in the list. And n is fixed with 3 times.

d={'A': [15,16, 17,19,20,21,25,26,27,28,29,30], 'B': [25,90,101,137,140,190,202,207,290,304,355,367]

df=pd.DataFrame(data=d)

The end result is to create a list=[15,17,25,28,30] based on another list of B values:[25, 101,202,304,367].

I previously set colA=[min(df.A)], and I'm trying to append the other three items based on the index of the colB list. And add the max column A value as the last item in the list colA.

So back to the other 3 items in colB, I'll need to do in the range(3) iteration. So when n=0, the first item is the first value that closed to (n 1)*100 but >(n 1)*100, same thing for the rest two values.

CodePudding user response:

If I understand you problem right, you can use df.groupby:

out = df.groupby(df["B"] // 100)["A"].first().to_list()   [df["A"].max()]
print(out)

Prints:

[15, 17, 25, 28, 30]
  • Related