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

分别举例说明,从列表、字典创建 DataFrame。

  1. User

    分别举例说明,从列表、字典创建 DataFrame。

  2. Assistant
    1. 从列表创建 DataFrame:
    import pandas as pd
    
    # 定义列表
    data = [[1, 'Alice', 25],
            [2, 'Bob', 30],
            [3, 'Charlie', 35]]
    
    # 定义列名
    columns = ['ID', 'Name', 'Age']
    
    # 使用列表和列名创建 DataFrame
    df = pd.DataFrame(data, columns=columns)
    
    print(df)
    

    输出结果:

       ID     Name  Age
    0   1    Alice   25
    1   2      Bob   30
    2   3  Charlie   35
    
    1. 从字典创建 DataFrame:
    import pandas as pd
    
    # 定义字典
    data = {'ID': [1, 2, 3],
            'Name': ['Alice', 'Bob', 'Charlie'],
            'Age': [25, 30, 35]}
    
    # 使用字典创建 DataFrame
    df = pd.DataFrame(data)
    
    print(df)
    

    输出结果:

       ID     Name  Age
    0   1    Alice   25
    1   2      Bob   30
    2   3  Charlie   35
    

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