Reputation: 309
I need to get for each row of a daframe, the first two digits of a number stored in another index (or column after a reset_index()). How can I do it?
My dataframe:
value
index1
110202 1
223168 5
850484 2
298008 3
950000 6
113500 6
849464 2
849616 10
I would like to obtain i.e:
value
index1 new_value
110202 11 1
223168 22 5
850484 85 2
298008 29 3
950000 95 6
113500 11 6
849464 84 2
849616 84 10
Upvotes: 7
Views: 27295
Reputation: 61910
Assuming index1
is the index of df
you could do:
df['new_value'] = df.index.astype(str).str[:2]
print(df)
Output
value new_value
index1
110202 1 11
223168 5 22
850484 2 85
298008 3 29
950000 6 95
113500 6 11
849464 2 84
849616 10 84
Basically convert the column to a string column and then use the str accessor to grab the first two characters. For more on working with text data, see here.
As an alternative you could reset the index and access the index1
column, for example:
df = df.reset_index()
df['new_value'] = df['index1'].astype(str).str[:2]
print(df.set_index(['index1', 'new_value']))
Output
value
index1 new_value
110202 11 1
223168 22 5
850484 85 2
298008 29 3
950000 95 6
113500 11 6
849464 84 2
849616 84 10
Notice that in this alternative solution I set the index as the columns new_value
and index1
.
Upvotes: 11
Reputation: 1956
make a list from the index with df.index.values then iterate over the values in this array and grab the first 2 characters
Upvotes: 1