Simon Breton
Simon Breton

Reputation: 2876

Pandas: rank() under groupby() returns "ValueError: Wrong number of items passed 2, placement implies 1"

I have the following dataframe:

index, col_name, extra_col
1, item_1, stuff
2, item_2, stuff
3, item_3, stuff
4, item_4, stuff
5, item_5, stuff
6, item_6, stuff
7, item_7, stuff
8, item_8, stuff
9, item_9, stuff

on which I'm applying the following transformation:

df = df.sort_values(by = 'col_name')
df['yPos'] = np.arange(len(df)) // 3
df['xPos'] = df.groupby(["yPos"]).rank(method='first')-1
df = df.astype({'x': 'int'})

I want to add two columns to my dataframe xPos and yPos. yPos column is working fine. however I have the following error when running the line with the xPos column:

Wrong number of items passed 2, placement implies 1

What's wrong?

Upvotes: 0

Views: 283

Answers (1)

SeaBean
SeaBean

Reputation: 23217

Your code produced results of multiple columns when you attempt to assign it to one column xPos. Hence, the error. You can instead rank only on yPos to get the relative serial number in xPos.

Change your codes as follows:

df = df.sort_values(by = 'col_name')
df['yPos'] = np.arange(len(df)) // 3

df['xPos'] = df.groupby("yPos")['yPos'].rank(method='first')-1
df = df.astype({'xPos': int})

Result:

print(df)

   index col_name extra_col  yPos  xPos
0      1   item_1     stuff     0     0
1      2   item_2     stuff     0     1
2      3   item_3     stuff     0     2
3      4   item_4     stuff     1     0
4      5   item_5     stuff     1     1
5      6   item_6     stuff     1     2
6      7   item_7     stuff     2     0
7      8   item_8     stuff     2     1
8      9   item_9     stuff     2     2

Upvotes: 2

Related Questions