dfのiterrowsの使い方
iterrows is a method in pandas that allows you to iterate over DataFrame rows as (index, Series) pairs. However, it’s important to note that using iterrows can be less efficient than other methods for large DataFrames, so it’s recommended to use it with caution, especially in performance-critical code.
Here’s a simple example of how to use iterrows:
python
import pandas as pd # Create a sample DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 22], 'City': ['New York', 'San Francisco', 'Los Angeles']} df = pd.DataFrame(data) # Iterate over rows using iterrows for index, row in df.iterrows(): print(f"Index: {index}, Name: {row['Name']}, Age: {row['Age']}, City: {row['City']}")
Output:
yaml
Index: 0, Name: Alice, Age: 25, City: New York Index: 1, Name: Bob, Age: 30, City: San Francisco Index: 2, Name: Charlie, Age: 22, City: Los Angeles
In each iteration, index represents the row index, and row is a pandas Series containing the data for that row.
Keep in mind that using vectorized operations in pandas is generally more efficient than iterating over rows with iterrows, so it’s a good practice to explore alternatives like using apply, vectorized operations, or the agg function depending on the specific task you’re trying to accomplish.

ディスカッション
コメント一覧
まだ、コメントがありません