Reputation: 403
In my workflow there are multiple CSVs with four columns OID, value, count, unique_id
. I am trying to figure how to generate incremental values under unique_id
column. Using apply()
, I can do something like df.apply(lambda x : x + 1) #where x = 0
and it will result in all values under unique_id
as 1. However, I am confused on how to use apply()
to generate incrementally values in each row for a specific column.
# Current Dataframe
OID Value Count unique_id
0 -1 1 5 0
1 -1 2 46 0
2 -1 3 32 0
3 -1 4 3 0
4 -1 5 17 0
# Trying to accomplish
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
Sample code (I understand that the syntax is incorrect, but it is approximately what I am trying to accomplish):
def numbers():
for index, row in RG_Res_df.iterrows():
return index
RG_Res_df = RG_Res_df['unique_id'].apply(numbers)
Upvotes: 4
Views: 11056
Reputation: 394159
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
Upvotes: 7