Rahul Shrivastava
Rahul Shrivastava

Reputation: 1411

Is there any more pythonic way to do the following repetitive code

I have more than 20 columns where I need to run following rule:

df['LAND1'] = df['LAND1'].str.replace('\W+', '')
df['LAND1'] = df['LAND1'].str.lower().astype(str)
df['SEA1'] = df['SEA1'].str.replace('\W+', '')
df['SEA1'] = df['SEA1'].str.lower().astype(str)
df['OCEAN1'] = df['OCEAN1'].str.replace('\W+', '')
df['OCEAN1'] = df['OCEAN1'].str.lower().astype(str)
df['CITY1'] = df['CITY1'].str.replace('\W+', '')
df['CITY1'] = df['CITY1'].str.lower().astype(str)

More same type of code for different Columns how can I minimize my code. Such that I could write less code.

Upvotes: 2

Views: 79

Answers (4)

B. M.
B. M.

Reputation: 18648

DataFrame.apply and DataFrame.applymap can also contract your code :

df=pd.DataFrame({'A':['a','b','c'],'D':['d','e','f'],'G':['g','h','i']})
   A  D  G  
0  a  d  g
1  b  e  h
2  c  f  i

Then:

df.apply(pd.Series.replace,args=('d','ddd')).applymap(str.upper)

   A    D  G
0  A  DDD  G
1  B    E  H
2  C    F  I

You can affect and restrict to some columns by selection=['A','D']; df[selection]=df[selection].apply(....) for example.

Upvotes: 1

user5497218
user5497218

Reputation: 26

Melt the data frame, then apply the repalce and to lower function. Pivot the data frame to get back

Upvotes: 0

Shrey
Shrey

Reputation: 1260

I hope the df is a dictionary

for i in df.keys():
    df[i]=df[i].str.replace('\W+', '')
    df[i]=df[i].str.lower().astype(str)

Let me know if it helps you

Upvotes: 0

Anand S Kumar
Anand S Kumar

Reputation: 90929

You can create a list of column names and then iterate through them and apply your logic for them. Example -

columns = ['LAND1','SEA1','OCEAN1','CITY1',...]
for col in columns:
    df[col] = (df[col].str.replace('\W+', '')
                      .str.lower().astype(str))

Demo -

In [17]: df
Out[17]:
         LAND1         SEA1
0  Blah!!!Bloh  Bleh@@@Blum
1  Blah!!!Bloh  Bleh@@@Blum
2  Blah!!!Bloh  Bleh@@@Blum
3  Blah!!!Bloh  Bleh@@@Blum
4  Blah!!!Bloh  Bleh@@@Blum
5  Blah!!!Bloh  Bleh@@@Blum
6  Blah!!!Bloh  Bleh@@@Blum
7  Blah!!!Bloh  Bleh@@@Blum
8  Blah!!!Bloh  Bleh@@@Blum
9  Blah!!!Bloh  Bleh@@@Blum

In [18]: columns = ['LAND1','SEA1']

In [20]: for col in columns:
   ....:     df[col] = (df[col].str.replace('\W+', '')
   ....:                       .str.lower().astype(str))
   ....:

In [21]: df
Out[21]:
      LAND1      SEA1
0  blahbloh  blehblum
1  blahbloh  blehblum
2  blahbloh  blehblum
3  blahbloh  blehblum
4  blahbloh  blehblum
5  blahbloh  blehblum
6  blahbloh  blehblum
7  blahbloh  blehblum
8  blahbloh  blehblum
9  blahbloh  blehblum

Upvotes: 4

Related Questions