Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

add an index column in range dataframe

df = df.loc[df.index.repeat(df['a'])]   
df['c'] = df.groupby(level=0).cumcount() + 1
df = df.reset_index(drop=True)
print (df)
   a  b  c
0  1  x  1
1  2  y  1
2  2  y  2
3  3  z  1
4  3  z  2
5  3  z  3
Comment

PREVIOUS NEXT
Code Example
Python :: pyton for 
Python :: python create empty dictionary with keys 
Python :: python array use numpy arange 
Python :: how to make a screen in pygame 
Python :: math in python 
Python :: Tree: Inorder Traversal 
Python :: round down number python 
Python :: python get an online file 
Python :: python re 
Python :: python string to boolean 
Python :: get dataframe name python 
Python :: how to store a return value in a variable in python 
Python :: where python packages are installed 
Python :: address already in use 
Python :: fraction in python 
Python :: how to find last element in array python 
Python :: avoid self python by making class functions static 
Python :: df.rename(index=str, columns={"A": "a", "C": "c"}) what does index=str means 
Python :: python if loop 
Python :: use of self in pythonic class 
Python :: hide password in python 
Python :: python how to add 2 numbers 
Python :: dataframe cut 
Python :: Odd number without loop in python 
Python :: python loop over list 
Python :: python bigquery example 
Python :: python one sample t-test 
Python :: editing specific line in text file in python 
Python :: get status code python 
Python :: pyhon 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =