Verschillende manieren om rijen in Pandas Dataframe te doorlopen

In dit artikel zullen we het bespreken hoe u rijen in een DataFrame in Pandas kunt doorlopen .

Hoe te itereren over rijen in een DataFrame in Pandas

Python is een geweldige taal voor het uitvoeren van data-analyse, vooral vanwege het fantastische ecosysteem van datagerichte Python-pakketten. Panda's is een van die pakketten en maakt het importeren en analyseren van gegevens veel eenvoudiger.

Laten we eens kijken naar de verschillende manieren om rijen in Panda's te herhalen Dataframe :

Methode 1: Het indexattribuut van het dataframe gebruiken.

Python3




# import pandas package as pd> import> pandas as pd> # Define a dictionary containing students data> data> => {> 'Name'> : [> 'Ankit'> ,> 'Amit'> ,> > 'Aishwarya'> ,> 'Priyanka'> ],> > 'Age'> : [> 21> ,> 19> ,> 20> ,> 18> ],> > 'Stream'> : [> 'Math'> ,> 'Commerce'> ,> > 'Arts'> ,> 'Biology'> ],> > 'Percentage'> : [> 88> ,> 92> ,> 95> ,> 70> ]}> # Convert the dictionary into DataFrame> df> => pd.DataFrame(data, columns> => [> 'Name'> ,> 'Age'> ,> > 'Stream'> ,> 'Percentage'> ])> print> (> 'Given Dataframe : '> , df)> print> (> ' Iterating over rows using index attribute : '> )> # iterate through each row and select> # 'Name' and 'Stream' column respectively.> for> ind> in> df.index:> > print> (df[> 'Name'> ][ind], df[> 'Stream'> ][ind])>

Uitgang:

Given Dataframe :  Name Age Stream Percentage 0 Ankit 21 Math 88 1 Amit 19 Commerce 92 2 Aishwarya 20 Arts 95 3 Priyanka 18 Biology 70  Iterating over rows using index attribute :  Ankit Math Amit Commerce Aishwarya Arts Priyanka Biology 

Methode 2: Gebruik makend van plaats[] functie van het Dataframe.

Python3




# import pandas package as pd> import> pandas as pd> # Define a dictionary containing students data> data> => {> 'Name'> : [> 'Ankit'> ,> 'Amit'> ,> > 'Aishwarya'> ,> 'Priyanka'> ],> > 'Age'> : [> 21> ,> 19> ,> 20> ,> 18> ],> > 'Stream'> : [> 'Math'> ,> 'Commerce'> ,> > 'Arts'> ,> 'Biology'> ],> > 'Percentage'> : [> 88> ,> 92> ,> 95> ,> 70> ]}> # Convert the dictionary into DataFrame> df> => pd.DataFrame(data, columns> => [> 'Name'> ,> 'Age'> ,> > 'Stream'> ,> > 'Percentage'> ])> print> (> 'Given Dataframe : '> , df)> print> (> ' Iterating over rows using loc function : '> )> # iterate through each row and select> # 'Name' and 'Age' column respectively.> for> i> in> range> (> len> (df)):> > print> (df.loc[i,> 'Name'> ], df.loc[i,> 'Age'> ])>

Uitgang:

Given Dataframe :  Name Age Stream Percentage 0 Ankit 21 Math 88 1 Amit 19 Commerce 92 2 Aishwarya 20 Arts 95 3 Priyanka 18 Biology 70  Iterating over rows using loc function :  Ankit 21 Amit 19 Aishwarya 20 Priyanka 18 

Methode 3: Gebruik makend van iloc[] functie van het DataFrame.

Python3




# import pandas package as pd> import> pandas as pd> # Define a dictionary containing students data> data> => {> 'Name'> : [> 'Ankit'> ,> 'Amit'> ,> > 'Aishwarya'> ,> 'Priyanka'> ],> > 'Age'> : [> 21> ,> 19> ,> 20> ,> 18> ],> > 'Stream'> : [> 'Math'> ,> 'Commerce'> ,> > 'Arts'> ,> 'Biology'> ],> > 'Percentage'> : [> 88> ,> 92> ,> 95> ,> 70> ]}> # Convert the dictionary into DataFrame> df> => pd.DataFrame(data, columns> => [> 'Name'> ,> 'Age'> ,> > 'Stream'> ,> 'Percentage'> ])> print> (> 'Given Dataframe : '> , df)> print> (> ' Iterating over rows using iloc function : '> )> # iterate through each row and select> # 0th and 2nd index column respectively.> for> i> in> range> (> len> (df)):> > print> (df.iloc[i,> 0> ], df.iloc[i,> 2> ])>

Uitgang:

Given Dataframe :  Name Age Stream Percentage 0 Ankit 21 Math 88 1 Amit 19 Commerce 92 2 Aishwarya 20 Arts 95 3 Priyanka 18 Biology 70  Iterating over rows using iloc function :  Ankit Math Amit Commerce Aishwarya Arts Priyanka Biology ​ 

Methode 4: Gebruik makend van iterrijen() methode van het Dataframe.

Python3




# import pandas package as pd> import> pandas as pd> # Define a dictionary containing students data> data> => {> 'Name'> : [> 'Ankit'> ,> 'Amit'> ,> > 'Aishwarya'> ,> 'Priyanka'> ],> > 'Age'> : [> 21> ,> 19> ,> 20> ,> 18> ],> > 'Stream'> : [> 'Math'> ,> 'Commerce'> ,> > 'Arts'> ,> 'Biology'> ],> > 'Percentage'> : [> 88> ,> 92> ,> 95> ,> 70> ]}> # Convert the dictionary into DataFrame> df> => pd.DataFrame(data, columns> => [> 'Name'> ,> 'Age'> ,> > 'Stream'> ,> 'Percentage'> ])> print> (> 'Given Dataframe : '> , df)> print> (> ' Iterating over rows using iterrows() method : '> )> # iterate through each row and select> # 'Name' and 'Age' column respectively.> for> index, row> in> df.iterrows():> > print> (row[> 'Name'> ], row[> 'Age'> ])>

Uitgang:

Given Dataframe :  Name Age Stream Percentage 0 Ankit 21 Math 88 1 Amit 19 Commerce 92 2 Aishwarya 20 Arts 95 3 Priyanka 18 Biology 70  Iterating over rows using iterrows() method :  Ankit 21 Amit 19 Aishwarya 20 Priyanka 18 

Methode 5: Gebruik makend van itertuples() methode van het dataframe.

Python3




# import pandas package as pd> import> pandas as pd> # Define a dictionary containing students data> data> => {> 'Name'> : [> 'Ankit'> ,> 'Amit'> ,> 'Aishwarya'> ,> > 'Priyanka'> ],> > 'Age'> : [> 21> ,> 19> ,> 20> ,> 18> ],> > 'Stream'> : [> 'Math'> ,> 'Commerce'> ,> 'Arts'> ,> > 'Biology'> ],> > 'Percentage'> : [> 88> ,> 92> ,> 95> ,> 70> ]}> # Convert the dictionary into DataFrame> df> => pd.DataFrame(data, columns> => [> 'Name'> ,> 'Age'> ,> > 'Stream'> ,> > 'Percentage'> ])> print> (> 'Given Dataframe : '> , df)> print> (> ' Iterating over rows using itertuples() method : '> )> # iterate through each row and select> # 'Name' and 'Percentage' column respectively.> for> row> in> df.itertuples(index> => True> , name> => 'Pandas'> ):> > print> (> getattr> (row,> 'Name'> ),> getattr> (row,> 'Percentage'> ))>

Uitgang:

Given Dataframe :  Name Age Stream Percentage 0 Ankit 21 Math 88 1 Amit 19 Commerce 92 2 Aishwarya 20 Arts 95 3 Priyanka 18 Biology 70  Iterating over rows using itertuples() method :  Ankit 88 Amit 92 Aishwarya 95 Priyanka 70 ​ 

Methode 6: Gebruik makend van toepassen() methode van het Dataframe.

Python3




# import pandas package as pd> import> pandas as pd> # Define a dictionary containing students data> data> => {> 'Name'> : [> 'Ankit'> ,> 'Amit'> ,> 'Aishwarya'> ,> > 'Priyanka'> ],> > 'Age'> : [> 21> ,> 19> ,> 20> ,> 18> ],> > 'Stream'> : [> 'Math'> ,> 'Commerce'> ,> 'Arts'> ,> > 'Biology'> ],> > 'Percentage'> : [> 88> ,> 92> ,> 95> ,> 70> ]}> # Convert the dictionary into DataFrame> df> => pd.DataFrame(data, columns> => [> 'Name'> ,> 'Age'> ,> 'Stream'> ,> > 'Percentage'> ])> print> (> 'Given Dataframe : '> , df)> print> (> ' Iterating over rows using apply function : '> )> # iterate through each row and concatenate> # 'Name' and 'Percentage' column respectively.> print> (df.> apply> (> lambda> row: row[> 'Name'> ]> +> ' '> +> > str> (row[> 'Percentage'> ]), axis> => 1> ))>

Uitgang:

Given Dataframe :  Name Age Stream Percentage 0 Ankit 21 Math 88 1 Amit 19 Commerce 92 2 Aishwarya 20 Arts 95 3 Priyanka 18 Biology 70  Iterating over rows using apply function :  0 Ankit 88 1 Amit 92 2 Aishwarya 95 3 Priyanka 70 dtype: object