python - how to add new item in pandas series without erasing other items -


i have following pandas series.

new_orders_list out[853]:  cluster 1    [525, 526, 533] cluster 2    [527, 528, 532] cluster 3    [519, 534, 535] cluster 4              [530] cluster 5         [529, 531] cluster 6    [520, 521, 524] 

and,i have 2 more series after slicing on dataframe.

condition out[854]:  5    525 name: order_id, dtype: object  condition2 out[855]:  clusters cluster 6    1 name: quant_bought, dtype: int64 

now want add value of condition series 525 new_orders_list @ cluster 6 (index condition2 series) location. , erasing off 525 cluster 1 location. so, should this

cluster 1    [526, 533] cluster 2    [527, 528, 532] cluster 3    [519, 534, 535] cluster 4              [530] cluster 5         [529, 531] cluster 6    [520, 521, 524, 525] 

i doing following in python. appends stored values.

new_orders_list.append(pd.series(condition.values ,index =  condition2.index))  cluster 1    [525, 526, 533] cluster 2    [527, 528, 532] cluster 3    [519, 534, 535] cluster 4              [530] cluster 5         [529, 531] cluster 6    [520, 521, 524] cluster 6                525 

you can try solution.

new series of remove data created , called remseries.

types of values in lists in series new_orders_list integers , types of other series strings, values converted strings.

then selected rows subsets isin , values added , removed.

print new_orders_list  clusters cluster 1    [525, 526, 533] cluster 2    [527, 528, 532] cluster 3    [519, 534, 535] cluster 4              [530] cluster 5         [529, 531] cluster 6    [520, 521, 524] name: no, dtype: object  print condition  5    525 name: order_id, dtype: object  print condition2  clusters cluster 6    1 name: quant_bought, dtype: int64  #create new series remove remseries = pd.series(condition.values, index = ['cluster 1'], name='rem') print remseries  cluster 1    525 name: rem, dtype: object 
#create dataframe series df = new_orders_list.reset_index() print df      clusters               no 0  cluster 1  [525, 526, 533] 1  cluster 2  [527, 528, 532] 2  cluster 3  [519, 534, 535] 3  cluster 4            [530] 4  cluster 5       [529, 531] 5  cluster 6  [520, 521, 524]  #convert values in list int string df['no'] = df['no'].apply(lambda x: [str(i) in x])  #add , remove items df.loc[df['clusters'].isin(condition2.index.tolist()), 'no'] =  df['no'].apply(lambda x: x + condition.values.tolist())  df.loc[df['clusters'].isin(remseries.index.tolist()), 'no']  =  df['no'].apply(lambda x: [k k in x if k != ''.join(remseries.values)])  #check types of values in list print [ type(x) x in df['no'][0]]  [<type 'str'>, <type 'str'>]  #convert values in list string int df['no'] = df['no'].apply(lambda x: [int(i) in x]) 
print df      clusters                    no 0  cluster 1            [526, 533] 1  cluster 2       [527, 528, 532] 2  cluster 3       [519, 534, 535] 3  cluster 4                 [530] 4  cluster 5            [529, 531] 5  cluster 6  [520, 521, 524, 525]  #check types of values in list print [ type(x) x in df['no'][0]]  [<type 'int'>, <type 'int'>] 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -