Home > other >  Create array in array python
Create array in array python

Time:10-26

I have a data frame like this:

ID Date Rank Info -
12 12/5 1 Anne
12 12/5 2 Louis
12 12/5 3 Nico
12 12/5 4 Vic
13 15/5 2 Stephanie
13 15/5 3 Athene
13 15/5 4 Louis

How can i create an array 'Rank' with condition same id and date?

Solution: [[1,2,3,4],[2,3,4]]

And i have another array [[1,3],[2,4]]. How do I declare the information of those who are located less than the value in this array?

Thank you so much!!!!!

CodePudding user response:

Use .agg:

res = df.groupby(["ID", "Date"])["Rank"].agg(list).tolist()
print(res)

Output

[[1, 2, 3, 4], [2, 3, 4]]

Alternative if numpy.array of list is the expected output:

res = df.groupby(["ID", "Date"])["Rank"].agg(list).to_numpy()
print(res)

Output

[list([1, 2, 3, 4]) list([2, 3, 4])]

CodePudding user response:

Dani Mesejo answer is correct, but I want to point that what you are calling arrays really are lists.

If you want to use arrays you must import array module or use NumPy arrays.

I would post this as a comment but unfortunately I haven't enough reputation to comment.

You can find more information about difference between lists and arrays in this link.

  • Related