Reputation: 63
Using pandas I want to be fill DVZ column with data (data1, data2, etc..) based on starting sting of column DV2 for example if start with 9 will add data1 to DVZ first raw and second if start with 7 will add data2 if start with 7 will be data3 in DVZ
I am trying with dew['DVZ']
[dew["DV0"].astype('str').str.startswith("9")] = "Sin"
but I'm not able to get the desired result. Appreciate if any one can help me
DV1 | DV2 | DVZ |
---|---|---|
9412 | 941 | new data |
9434 | 911 | new data |
9412 | 717 | new data |
3114 | 311 | new data |
6314 | 631 | new data |
6622 | 662 | new data |
Here's my desired result:
DV1 | DV2 | DVZ |
---|---|---|
9412 | 941 | data1 |
9434 | 911 | data1 |
9412 | 717 | data2 |
3114 | 311 | data3 |
6314 | 631 | data4 |
6622 | 662 | data4 |
Upvotes: 0
Views: 64
Reputation: 120519
IIUC, you can use:
c = df['DV2'].astype(str).str[0]
df['DVZ'] = 'data' + c.ne(c.shift()).cumsum().astype(str)
print(df)
# Output
DV1 DV2 DVZ
0 9412 941 data1
1 9434 911 data1
2 9412 717 data2
3 3114 311 data3
4 6314 631 data4
5 6622 662 data4
Update
Because DV2 is start with 9 I will add the word data1 when start with 7 I will put date2 with start with 4 I put data3 and so on.
Create a mapping dict:
M = {'9': 'data1', '7': 'data2', '3': 'data3', '6': 'data4'} # and so on
df['DVZ'] = df['DV2'].astype(str).str[0].map(M)
print(df)
# Output
DV1 DV2 DVZ
0 9412 941 data1
1 9434 911 data1
2 9412 717 data2
3 3114 311 data3
4 6314 631 data4
5 6622 662 data4
Upvotes: 1