[英]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: 我有20列以上需要运行以下规则的列:
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.
这样我可以写更少的代码。
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
DataFrame.apply
and DataFrame.applymap
can also contract your code : DataFrame.apply
和DataFrame.applymap
也可以收缩您的代码:
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(....)
您可以通过
selection=['A','D']; df[selection]=df[selection].apply(....)
来影响和限制某些列selection=['A','D']; df[selection]=df[selection].apply(....)
selection=['A','D']; df[selection]=df[selection].apply(....)
for example. selection=['A','D']; df[selection]=df[selection].apply(....)
。
I hope the df is a dictionary
我希望df是一
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 让我知道是否对您有帮助
Melt the data frame, then apply the repalce and to lower function. 融化数据框,然后将其应用于下层功能。 Pivot the data frame to get back
旋转数据框以返回
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.