Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 修改循环内理解的值_Python_Pandas_Loops - Fatal编程技术网

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谢谢它被更新为非常棒的。伟大的我没有想到这一点。当然,因为我不经常使用它。再次感谢