How to get rid of first element of store id using pandas?

Abdulkadir Lokhandwala

My data-frame consists of StoreId which needs to be changed for a particular type of store:

StoreType  StoreId
 A           105
 A           213
 B           401
 B           402
 B           711
 B           910
 B           913
 B           915

In this dataframe, just for storeType = B, I want to get rid of all the 4's if the storeId starts with 4,(for example, 401 should change to 01, 402 should change to 02), for any other StoreID with storetype = B, there is no such logic and hence it needs to be hard coded like 711 should change to I0, 910 to 801, 913 to 804, 915 to 814.

How can I write an efficient code using pandas data-frame in python??

user3483203

You can use a simple regular expression here, along with where to only change columns where a B is found in the other series.


u = df.StoreId.astype(str)
df.assign(StoreId=u.where(df.StoreType.ne('B'), u.str.replace('^4', '')))

  StoreType StoreId
0         A     105
1         A     213
2         B      01
3         B      02
4         B     711
5         B     910
6         B     913
7         B     915

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get rid of excessive top margin in the first element of a printed page

How to get python list first element id

How to get rid of multilevel index after using pivot table pandas?

How do I get rid of the outside values in this graph using pandas?

Thymeleaf + Spring: get rid of the default element id

How to get first number in element using jQuery

Flutter: How to get rid of the first touch delay?

How to get Store Name by using Multiple Store Id in Magento?

Using spacy to get rid of stopwords in pandas series

Javascript Redux - how to get an element from store by id

How to get rid of this element or change its color?

How to Get the ID of an HTML element using PHP?

How to get html element id using jquery

How to get page element by id using QWebEngineView

How to get rid of multiple branch_id?

How can I get rid of settingwithcopywarning pandas

How to select first child of parent by using ID of a child element?

How to get element value using minidom in python and store them into a list?

How to get rid of space below element inside details element

how can I get the first unique ID element and last unique ID element in an array React js

How to get rid of Series heading (column heading) using Pandas Library in Python

How to get rid of the value using a Gson TypeAdapter?

How to get rid of ObjectFactory in code using StructureMap

How to get rid of urls while using TfidfVectorizer

How to get rid of let using before

How to get rid of " " when using fputcsv?

How to Get A Element First Child Text Using Jquery And Class Selector?

how to get first element in angular 5 using ngFor directive?

How to get the first element result in Php using foreach?