{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Pandas_Strings\n","\n","https://pandas.pydata.org/docs/"],"metadata":{"id":"mdZekVgtBvU4"}},{"cell_type":"markdown","source":["## Import Pandas"],"metadata":{"id":"6bStA7ACB3nJ"}},{"cell_type":"code","source":["import pandas as pd"],"metadata":{"id":"rp6BfYkZB3vb"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## Problem 1: Basic String Cleaning and Extraction\n","\n","Clean the review text using Pandas string methods.\n","\n","\n","1.   Remove leading and trailing whitespace.\n","2.   Convert all text to lowercase.\n","3.  Remove all punctuation.\n","4. Extract the first three words of each review and store them in a new column short_review.\n","\n","\n","\n","\n","\n","\n","\n"],"metadata":{"id":"rxAYKBBZB6lU"}},{"cell_type":"code","source":["import pandas as pd\n","#Creating the dataframe\n","data = {\n","    \"review_id\": [101, 102, 103],\n","    \"review_text\": [\n","        \" Great product! Loved it. \",\n","        \"Horrible service!!! never again...\",\n","        \"Okayish item, not bad - not great.\"\n","    ]\n","}\n","\n","df_reviews = pd.DataFrame(data)"],"metadata":{"id":"OYnjjpAAB6tm"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Whitespaces"],"metadata":{"id":"U9AtHlQfCdg_"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Lowercase"],"metadata":{"id":"rsGMMXNDCdmz"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Punctuation"],"metadata":{"id":"N4NPJ8zTCdqM"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Extraction"],"metadata":{"id":"W6RDrXF8Cdt1"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## Problem 2: Filtering and Substring Pattern Matching\n","\n","Using the cleaned review_text from Problem 1, perform the following operations:\n","1. Add a column contains_positive which is True if the review contains the words \"great\" or \"loved\", and False otherwise.\n","2. Add a column contains_negative which is True if the review contains the word \"never\" or \"bad\", and False otherwise.\n","3. Filter the DataFrame to only show reviews that are positive and not negative."],"metadata":{"id":"6g_P_oGVCk84"}},{"cell_type":"code","source":["# contains_positive"],"metadata":{"id":"XXvnD8tBClCv"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# contains_negative"],"metadata":{"id":"CkLgrePSC8Ps"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Filter for postive and not negative"],"metadata":{"id":"0VLl2W_BC8Sj"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Pandas Sorting"],"metadata":{"id":"PzZ0CYNuF4h8"}},{"cell_type":"markdown","source":["## Problem 3: Space Tourism Rankings\n","\n","\n","- Sort the DataFrame by SatisfactionScore in descending order.\n","- Then, sort the DataFrame by Destination alphabetically, and within each Destination, sort by TripDuration ascending.\n","- Finally, create a new column CostPerHour (PriceUSD / TripDuration), and sort by that in ascending order.\n","\n"],"metadata":{"id":"QnFexLdmF65A"}},{"cell_type":"code","source":["data = {\n","    'CustomerID': [101, 102, 103, 104, 105, 106],\n","    'Destination': ['Moon', 'Mars', 'Moon', 'ISS', 'Mars', 'ISS'],\n","    'TripDuration': [12, 300, 14, 5, 310, 6],  # duration in hours\n","    'SatisfactionScore': [8.7, 9.5, 8.5, 9.0, 9.6, 8.9],  # out of 10\n","    'PriceUSD': [150000, 450000, 145000, 70000, 460000, 68000]\n","}\n","\n","df_space = pd.DataFrame(data)"],"metadata":{"id":"zwK44x4oF4pY"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Sort by satisfaction"],"metadata":{"id":"j-_rYWrhGJrv"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Sort by destination, then duration"],"metadata":{"id":"jwgkSnb2GJyz"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Create a new column and sort"],"metadata":{"id":"3VWjLnL0GJ12"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## Problem 4: The Game Leaderboard\n","Sort the leaderboard such that\n","\n","- Players are ranked by number of wins (descending).\n","- If there is a tie in wins, rank them by average match time (ascending) — the lower, the better.\n","- After sorting, reset the index and return the sorted DataFrame."],"metadata":{"id":"5g8YUhkYH0SH"}},{"cell_type":"code","source":["data = {\n","    'Username': ['PixelPirate', 'ShadowFox', 'QuantumQueen', 'KnightRider', 'BlitzBolt'],\n","    'Wins': [58, 74, 74, 60, 74],\n","    'AvgTime': [12.5, 10.2, 15.0, 11.8, 9.9]\n","}\n","\n","df_leaderboard = pd.DataFrame(data)"],"metadata":{"id":"a8s323lZH0Z6"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Sort by wins then avgtime"],"metadata":{"id":"z1xYc8DNICBq"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Reset the index"],"metadata":{"id":"5JcHMvfMICIg"},"execution_count":null,"outputs":[]}]}