簡體   English   中英

修改循環理解的值

[英]Modify values of in loop comprehension

如果該單元格的長度大於一個,我正在嘗試在該單元格中添加一個字符串。 我這樣做了,所以我在整個專欄中有兩組字符串。 這將有助於使用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


 .... 

output 會這樣:

 store
 Le rateau , 37 rue Lagarde
 Cordonnerie Fabien, 42 penasse
 'NA', 33 Charles de Gaule 
 LeClerc, 238 Le rabais

通過 boolean 屏蔽嘗試:

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

output 的df

    store
0   Le rateau , 37 rue Lagarde
1   Cordonnerie Fabien, 42 penasse
2   NA, 33 Charles de Gaule
3   LeClerc, 238 Le rabais

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM