Home > database >  Add new column with vlaue based on other colum starting charchter
Add new column with vlaue based on other colum starting charchter

Time:07-01

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 not able to reach desire result . appreciate if any one can hint 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 hope result

DV1 DV2 DVZ
9412 941 data1
9434 911 data1
9412 717 data2
3114 311 data3
6314 631 data4
6622 662 data4

CodePudding user response:

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
  • Related