स्ट्रिंग पैटर्न निकालना
hiking डेटासेट का Length कॉलम स्ट्रिंग्स का कॉलम है, लेकिन उसके भीतर हाइक का mileage मौजूद है. हम इस mileage को रेग्युलर एक्सप्रेशन्स से निकालेंगे, और फिर pandas में एक lambda का इस्तेमाल करके इस extraction को DataFrame पर लागू करेंगे.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Machine Learning के लिए Preprocessing
अभ्यास निर्देश
lengthआर्ग्युमेंट के टेक्स्ट में नंबर और decimals को किसी उपयुक्त पैटर्न से खोजें.- मैच हुए पैटर्न को निकालें और उसे float में बदलें.
return_mileage()फंक्शन कोhiking["Length"]कॉलम की हर पंक्ति पर लागू करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Write a pattern to extract numbers and decimals
def return_mileage(length):
# Search the text for matches
mile = re.____(____, ____)
# If a value is returned, use group(0) to return the found value
if mile is not None:
return float(____)
# Apply the function to the Length column and take a look at both columns
hiking["Length_num"] = ____.apply(____)
print(hiking[["Length", "Length_num"]].head())