Skip to content
Functions In Python
  • AI Chat
  • Code
  • Report
  • Python Data Science Toolbox

    Run the hidden code cell below to import the data used in this course.

    # Import the course packages
    import pandas as pd
    from functools import reduce
    
    # Import the dataset
    tweets = pd.read_csv('datasets/tweets.csv')

    Explore Datasets

    Use the DataFrame imported in the first cell to explore the data and practice your skills!

    • Write a function that takes a timestamp (see column timestamp_ms) and returns the text of any tweet published at that timestamp. Additionally, make it so that users can pass column names as flexible arguments (*args) so that the function can print out any other columns users want to see.
    • In a filter() call, write a lambda function to return tweets created on a Tuesday. Tip: look at the first three characters of the created_at column.
    • Make sure to add error handling on the functions you've created!
    tweets.head()
    def get_tweet_by_timestamp(timestamp, *args):
        try:
            # Filter the DataFrame for the given timestamp
            tweet_row = tweets[tweets['timestamp_ms'] == timestamp]
            
            # Check if any tweet exists for the given timestamp
            if tweet_row.empty:
                return "No tweet found for the given timestamp."
            
            # Get the text of the tweet
            tweet_text = tweet_row['text'].values[0]
            
            # Prepare the result dictionary
            result = {'text': tweet_text}
            
            # Add any additional columns requested
            for arg in args:
                if arg in tweets.columns:
                    result[arg] = tweet_row[arg].values[0]
                else:
                    result[arg] = "Column not found"
            
            return result
        
        except Exception as e:
            return f"An error occurred: {e}"
    get_tweet_by_timestamp(1234567890, 'created_at', 'user')
    import pandas as pd
    
    # Function to check if a tweet was created on a Tuesday
    def is_tuesday(created_at):
        try:
            return created_at[:3] == 'Tue'
        except Exception as e:
            return False
    
    # Apply the filter to get tweets created on a Tuesday
    tuesday_tweets = tweets[tweets['created_at'].apply(lambda x: is_tuesday(x))]
    
    tuesday_tweets.head()