Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

遍历dataframe

  1. User

    遍历dataframe

  2. Assistant

    遍历DataFrame可以使用iterrows()方法,示例代码如下:

    import pandas as pd
    
    # 创建DataFrame
    data = {'Name': ['Tom', 'Nick', 'John', 'Mike'],
            'Age': [28, 32, 25, 40],
            'City': ['London', 'New York', 'Paris', 'Tokyo']}
    df = pd.DataFrame(data)
    
    # 使用iterrows()遍历DataFrame
    for index, row in df.iterrows():
        print(f"Index: {index}, Name: {row['Name']}, Age: {row['Age']}, City: {row['City']}")
    

    输出结果:

    Index: 0, Name: Tom, Age: 28, City: London
    Index: 1, Name: Nick, Age: 32, City: New York
    Index: 2, Name: John, Age: 25, City: Paris
    Index: 3, Name: Mike, Age: 40, City: Tokyo
    

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