[英]Remove the row from dataframe, that has string length greater than a certain number, after a certain character(“,”) till end
[英]Remove the rows from pandas dataframe, that has sentences longer than certain word length
我想從pandas數據幀中刪除行,其中包含長度大於所需長度的特定列的字符串。
例如:
輸入框:
X Y
0 Hi how are you.
1 An apple
2 glass of water
3 I like to watch movie
現在,假設我要從數據幀中刪除具有長度大於或等於4的字符串的行。
所需的輸出幀必須是:
X Y
1 An apple
2 glass of water
刪除列'X'中值為0,3的行,因為列0中的字數為4,列3分別為5。
首先按空格分割值,通過Series.str.len
獲取行數,並通過反轉條件檢查>=
to <
with Series.lt
for boolean indexing
:
df = df[df['Y'].str.split().str.len().lt(4)]
#alternative with inverted mask by ~
#df = df[~df['Y'].str.split().str.len().ge(4)]
print (df)
X Y
1 1 An apple
2 2 glass of water
你可以計算空間:
df[df.Y.str.count('\s+').lt(3)]
X Y
1 1 An apple
2 2 glass of water
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.