简体   繁体   中英

Some calculations in pandas with the addition of a column

I have a table that has a column "Col1" that looks something like this:

| Col1 |

| 2 |

| 2 |

| 4 |

| 4 |

| 4 |

| 4 |

| 3 |

| 3 |

| 3 |

| 3 |

| 3 |

| 3 |

I need to create a new column "Col2". The table after this should look like this:

| Col1 | Col2 |

| 2 | 1 |

| 2 | 2 |

| 4 | 1 |

| 4 | 2 |

| 4 | 3 |

| 4 | 4 |

| 3 | 1 |

| 3 | 2 |

| 3 | 3 |

| 3 | 1 |

| 3 | 2 |

| 3 | 3 |

Is it possible to make so that if I have the same values in a row, the code starts from 1? As for example with 3.

| 3 | 1 |

| 3 | 2 |

| 3 | 3 |

| 3 | 1 |

| 3 | 2 |

| 3 | 3 |

Let's try this pandas solution without looping:

df2 = df.assign(Col2=df.groupby('Col1')['Col1'].cumcount().mod(df['Col1']).add(1))
print(df2)

Output:

    Col1  Col2
0      2     1
1      2     2
2      4     1
3      4     2
4      4     3
5      4     4
6      3     1
7      3     2
8      3     3
9      3     1
10     3     2
11     3     3
import pandas as pd
df = pd.DataFrame({'Col1':[2,2,4,4,4,4,3,3,3,3,3,3]})
i = 0
Col2 = []
Col1 = df.Col1

#Construct Col2 
while i < (len(Col1)):
    Col2.extend(list(range(1,Col1[i]+1)))
    i = len(Col2)
#Add Col2 to Dataframe
df['Col2'] = Col2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM