我正在嘗試自學 Python 進行資料分析,并練習我正在分析一個包含 27k 調查回復的 CSV 檔案。
回復采用“# - 評分”的形式(例如:“10 - 非常感興趣”)
誰能告訴我如何洗掉除數值以外的所有內容,以便我可以用 matplotlib 繪制這些資料?
謝謝 :)
編輯:抱歉,這是我正在使用的 df 的代碼:
likely_recc = pd.read_csv('test_data.csv', usecols = (['How likely are you to recommend this product to a friend?']))
uj5u.com熱心網友回復:
這應該可以解決問題
df['col'].str.split(' ').str[0].astype(int)
查看有關字串方法的 pandas 檔案以獲取更多資訊。
uj5u.com熱心網友回復:
input_array = ["10 - Extremely Interested", "9 - Very Interested"]
only_my_numbers = []
for element in input_array:
# element.split("-") will split based on "-"
# But it will will have trailing space. Remove that with strip()
print(element.split("-")[0].strip())
only_my_numbers.append(element.split("-")[0].strip())
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/494581.html
