I have problems with a for loop on two columns of my dataframe. With this for loop I want to add a new column as I show you in this example:
I start with this dataframe:
person store_id
0 Rose 24256
1 Tim 24257
2 Tim 27896
3 Tim 29547
4 Jack 24258
5 Jack 27897
and the new column I would like to add should look like this. In words I want to add a column showing a sort of enumeration of the stores visited by each person.
person store_id enum
0 Rose 24256 1
1 Tim 24257 1
2 Tim 27896 2
3 Tim 29547 3
4 Jack 24258 1
5 Jack 27897 2
This is the code I tried, but I do not get what I want.
for x in df['person']:
i = 1
for x in df['store_id']:
df['enum'] = i
i = i + 1
I have also tried as follows, getting again a wrong result:
for x,y in zip(df['person'], df['store_id']):
i = 1
df['enum'] = i
i = i + 1
What am I doing wrong? Thank you for your help!
No need to iterate over multiple columns; iterate with iterrows
and use a dictionary to track visited store id position "enum", then you set the enum column:
import pandas as pd
import numpy as np
df = pd.DataFrame(data={"person":[ 'Rose', 'Tim', 'Tim', 'Tim', 'Jack', 'Jack' ],
"store_id": [1580407549, 1580557630, 1580571344, 1580647900, 1580755371, 1580911127]})
store_index = {}
enums = []
for index, row in df.iterrows():
sid = row['person']
if sid not in store_index:
store_index[sid] = 0
store_index[sid] += 1
enums.append(store_index[sid])
df['enum'] = enums
print(df)
Output:
person store_id enum
0 Rose 1580407549 1
1 Tim 1580557630 1
2 Tim 1580571344 2
3 Tim 1580647900 3
4 Jack 1580755371 1
5 Jack 1580911127 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.