How to Add Incremental Numbers to a New Column Using Pandas
Here:
df = df.reset_index()
df = df.rename(columns={"index":"New_ID"})
df['New_ID'] = df.index + 880
Pandas creating incremental values in new column based on certain conditions
One approach:
ranks = df.sort_values(by=["Rank"],
key=lambda x: x.str.replace(r"\D+", "", regex=True).astype(int))\
.groupby("Name").transform("cumcount") + 1
ranks = ranks.apply("P{}".format)
df["NewIncremental"] = ranks
print(df)
Output
Name Rank Months NewIncremental
0 A A1 2 P1
1 A A1 2 P2
2 A A2 3 P3
3 A A2 3 P4
4 A A2 3 P5
5 B A1 4 P1
6 B A1 4 P2
7 B A1 4 P3
8 B A1 4 P4
9 C A3 2 P2
10 C A3 2 P3
11 C A2 1 P1
Step-by-step
# sort df by the given criteria, then group-by
sorted_by_rank = df.sort_values(by=["Rank"], key=lambda x: x.str.replace(r"\D+", "", regex=True).astype(int))
# get the ranks and apply the expected format
ranks = sorted_by_rank.groupby("Name").transform("cumcount") + 1
ranks = ranks.apply("P{}".format)
# assign the new column
df["NewIncremental"] = ranks
print(df)
Pandas Dataframe - Generate incremental values
don't loop you can just directly assign a numpy array to generate the id, here using np.arange
and pass the num of rows which will be df.shape[0]
In [113]:
df['unique_id'] = np.arange(df.shape[0])
df
Out[113]:
OID Value Count unique_id
0 -1 1 5 0
1 -1 2 46 1
2 -1 3 32 2
3 -1 4 3 3
4 -1 5 17 4
or pure pandas method using RangeIndex
, here the default start
is 0
so we only need to pass stop=df.shape[0]
:
In [114]:
df['unique_id'] = pd.RangeIndex(stop=df.shape[0])
df
Out[114]:
OID Value Count unique_id
0 -1 1 5 0
1 -1 2 46 1
2 -1 3 32 2
3 -1 4 3 3
4 -1 5 17 4
How to increment number in pandas column from bottom to top on certain conditions
Let df
is your DataFrame. Observed that, data is already sorted by SR.No
and Date
columns.
threshold_date = '01-03-2015'
df['Date'] = pd.to_datetime(df['Date'])
starters = df[df['Date'] == threshold_date].index
df.loc[starters, 'Res'] = 1
j = 0
for i in starters:
_index = list(range(2, i+2-j))
_index.reverse()
df.loc[j:i-1, 'Res'] = _index
j = i+1
df.loc[df['Date'] > '01-03-2015', 'Res'] = 0
It will produce your required output
create new column of incremental number based on 2 categorical columns pandas dataframe
You can use cumcount
after groupby on username
and phase
.
df['count'] = df.groupby(['username', 'phase']).cumcount()+1
print(df)
username phase count
0 andrew 1 1
1 andrew 1 2
2 alex 1 1
3 alex 2 1
4 andrew 1 3
5 cindy 3 1
6 alex 2 2
Add incremeting number to certain coulmns of a pandas DataFrame
If your category
column is sorted, we can use GroupBy.cumcount
:
df['incrNbr'] = df.groupby('category')['category'].cumcount().add(1)
val category incrNbr
idx
9 30 a 1
8 40 a 2
7 50 b 1
6 60 b 2
5 70 c 1
4 80 c 2
3 90 c 3
Pandas Add an incremental number based on another column
Try with diff
then cumsum
df['run'] = df['sequence'].diff().ne(1).cumsum()
Out[349]:
0 1
1 1
2 1
3 1
4 1
5 2
6 2
7 2
8 3
9 3
10 3
11 3
12 3
13 3
14 3
Name: sequence, dtype: int32
Related Topics
Printing Lists as Tabular Data
How to Sleep Selenium Webdriver in Python For Milliseconds
Adding a Method to an Existing Object Instance
Using Python'S Eval() Vs. Ast.Literal_Eval()
What Are Iterator, Iterable, and Iteration
How to Find All Occurrences of an Element in a List
Python Regex Find All Overlapping Matches
How Does the @Property Decorator Work in Python
Qtdesigner Changes Will Be Lost After Redesign User Interface
Why Dict.Get(Key) Instead of Dict[Key]
Difference Between Old Style and New Style Classes in Python
Tkinter Creating Buttons in For Loop Passing Command Arguments
How to Add Value Labels on a Bar Chart
Is There Go Up Line Character? (Opposite of \N)
Standard_Init_Linux.Go:178: Exec User Process Caused "Exec Format Error"
Imagemagick Not Authorized to Convert Pdf to an Image
How to Sort a List of Dictionaries by a Value of the Dictionary