Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

add a nested list into a df

  1. User

    add a nested list into a df

  2. Assistant

    To add a nested list into a DataFrame in Python, you can use the pd.DataFrame function from the pandas library. Here's an example:

    import pandas as pd
    
    # Create a nested list
    nested_list = [['John', 25, 'USA'], ['Alice', 30, 'Canada'], ['Bob', 35, 'UK']]
    
    # Create a DataFrame from the nested list
    df = pd.DataFrame(nested_list, columns=['Name', 'Age', 'Country'])
    
    # Print the DataFrame
    print(df)
    

    Output:

       Name  Age Country
    0  John   25     USA
    1 Alice   30  Canada
    2   Bob   35      UK
    

    In this example, we have a nested list consisting of three sublists, each representing a row in the DataFrame. The pd.DataFrame function converts this nested list into a DataFrame. We provide the column names in the columns parameter to label the DataFrame's columns.

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