Reputation: 488
There is a Unique Key Column in my csv file and it has in every row the value 1. I want to replace them by real unique values (1,2,3,4,5 ....).
I try:
data=pd.read_csv(csv_file)
data['Unique Key'] = data['Unique Key'].replace(1:range(1))
but obviously doesn't work ;(
Upvotes: 1
Views: 991
Reputation: 323326
May using reset_index()
data['Unique Key'] = data.reset_index().index+1
Upvotes: 2
Reputation: 59579
Since it's a column of 1
s
df['Unique Key'] = df['Unique Key'].cumsum()
Upvotes: 3
Reputation: 5958
data['Unique Key'] = np.arange(len(data))
each column in the pd.DataFrame
can be replaced/ created by a numpy array of the same length.
If you want the keys to start from 1
, you can do
data['Unique Key'] = np.arange(len(data)) + 1
Upvotes: 5