Python 修改循环内理解的值
如果单元格的长度超过一,我将尝试在单元格中添加字符串。我这样做了,所以在整个列中有两组字符串。这将有助于使用Python 修改循环内理解的值,python,pandas,loops,Python,Pandas,Loops,如果单元格的长度超过一,我将尝试在单元格中添加字符串。我这样做了,所以在整个列中有两组字符串。这将有助于使用str.split()创建新列。以下是我正在做的: [i if len(df.address.str.split())>1 else 'NA '+ i for i in df.address ] 以下是数据示例: store Le rateau , 37 rue Lagarde Cordonnerie Fabien, 42 penasse 33 Charles de Gau
str.split()
创建新列。以下是我正在做的:
[i if len(df.address.str.split())>1 else 'NA '+ i for i in df.address ]
以下是数据示例:
store
Le rateau , 37 rue Lagarde
Cordonnerie Fabien, 42 penasse
33 Charles de Gaule # I want to add to them ones So I can have store & address later
LeClerc, 238 Le rabais
....
输出如下所示:
store
Le rateau , 37 rue Lagarde
Cordonnerie Fabien, 42 penasse
'NA', 33 Charles de Gaule
LeClerc, 238 Le rabais
尝试通过布尔掩蔽:
m=df['store'].str.split(',').str.len().eq(1)
#splitted the values by ',' and cheking if its length is 1
最后,通过面具:
df.loc[m,'store']='NA, '+df.loc[m,'store']
#passing the boolean series(that we stored in m variable) in loc accessor and
#modifying the values of store column where the above condition satisfying
df的输出
:
store
0 Le rateau , 37 rue Lagarde
1 Cordonnerie Fabien, 42 penasse
2 NA, 33 Charles de Gaule
3 LeClerc, 238 Le rabais
我将帮助展示一个预期结果的示例output@joao谢谢它被更新为非常棒的。伟大的我没有想到这一点。当然,因为我不经常使用它。再次感谢