Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-2.1· AnthropicHugging Face Arena Human Preference 55K

Show how to extract data from a pandas data frame column that contains json

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-3.5-turbo-1106’s answer.
  1. User

    Show how to extract data from a pandas data frame column that contains json

  2. Claude Assistant

    Here is an example of how to extract data from a pandas DataFrame column that contains JSON:

    import pandas as pd
    
    data = [{'name': 'John', 'age': 20}, 
            {'name': 'Sarah', 'age': 25}]
    
    df = pd.DataFrame(data)
    
    # The 'data' column contains JSON 
    print(df)
                                                   data
    0  {'name': 'John', 'age': 20}
    1  {'name': 'Sarah', 'age': 25}
    
    # Use json_normalize to extract nested data
    df = pd.json_normalize(df['data'])
    
    print(df)
       name  age
    0  John   20 
    1 Sarah   25
    

    The key steps are:

    1. Create a DataFrame with a column containing JSON data
    2. Use pd.json_normalize and pass the JSON column to it
    3. This will extract the nested data into a new DataFrame

    The extracted data is now in its own columns in the DataFrame. This makes it easier to analyze compared to keeping it nested in a JSON string.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.