[英]Replace text in a pandas dataframe column with regex
我有一個 dataframe 有兩列:“名稱”和“分數和評論”。
“分數和評論”列中的每個值都將從以下 3 種情況之一開始:
我想更改“評分和評論”列的值,以便:
我曾嘗試查看正則表達式,但無論我從錯誤的角度接近這個問題,我都無法理解。
我已經嘗試過 myDataFrame.replace('[0-9]{1,3}\s*', '') 但我能得到的最接近的是它匹配前 3 個字符(如果它們是數字)並擺脫那些.
這是str.extract
的一個很好的用例,它使用正則表達式並且只留下匹配組。 例如:
>>> x = pd.Series(["100 some text", "1", "123", "text that should be 0"])
>>> x.str.extract(r'(^[0-9]{1,3})').fillna(0)
0
0 100
1 1
2 123
3 0
因此,假設您不需要擔心不是 0-999 的數字,您可以這樣做:
myDataFrame["Score and comment"].str.extract(r'(^[0-9]{1,3})').fillna(0)
我將一些示例數據放入 csv 文件中,如下所示:
Name,Score and comment
Amun,123 this is cool
mirjam,23 this is nice
munkel,2 that's just amazing
punkel,this is funny
Rolf,123
Rolf,2
Mirjam2,17
Mirjaa,das ist gut
然后我運行了以下代碼:
import pandas as pd
df = pd.read_csv("/filepath/sample_data.txt")
score = df["Score and comment"]
#here, you first convert the cells that contain only numbers,
#then, you delete the strings after the numbers in the mixed cells
#and finally, you set the cells containing only strings to 0
for i in range(0,len(score)):
try:
score[i] = int(score[i])
except:
try:
score[i] = score[i].split(" ")[0]
score[i] = int(score[i])
except:
score[i] = 0
#save the file to a new csv
df.to_csv("/filepath/sample_data_convertet.txt", index = False)
output 如下:
Name,Score and comment
Amun,123
mirjam,23
munkel,2
punkel,0
Rolf,123
Rolf,2
Mirjam2,17
Mirjaa,0
它對我來說很好:-)我希望這會有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.