python - Comment itérer sur les rangs dans un DataFrame dans Pandas

Mots clés : pythonpandasdataframepython

meilleur 4 Réponses python - Comment itérer sur les rangs dans un DataFrame dans Pandas

vote vote

92

import pandas as pd  df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})  for index, row in df.iterrows():     print(row['c1'], row['c2']) 
10 100 11 110 12 120 
vote vote

88

# Iterating over one column - `f` is some function that processes your data result = [f(x) for x in df['col']] # Iterating over two columns, use `zip` result = [f(x, y) for x, y in zip(df['col1'], df['col2'])] # Iterating over multiple columns - same data type result = [f(row[0], ..., row[n]) for row in df[['col1', ...,'coln']].to_numpy()] # Iterating over multiple columns - differing data type result = [f(row[0], ..., row[n]) for row in zip(df['col1'], ..., df['coln'])] 
vote vote

74

  for index, row in df.iterrows():       print(row["c1"], row["c2"]) 
  for row in df.itertuples(index=True, name='Pandas'):       print(row.c1, row.c2) 
    new_df = df.apply(lambda x: x * 2) 
vote vote

60

df = pd.DataFrame({'a': randn(1000), 'b': randn(1000),'N': randint(100, 1000, (1000)), 'x': 'x'})  %timeit [row.a * 2 for idx, row in df.iterrows()] # => 10 loops, best of 3: 50.3 ms per loop  %timeit [row[1] * 2 for row in df.itertuples()] # => 1000 loops, best of 3: 541 µs per loop 

Questions similaires