Pandas Dataframe에서 행을 반복하는 다양한 방법

이 기사에서는 다음 내용을 다룰 것입니다. Pandas의 DataFrame에서 행을 반복하는 방법 .

Pandas의 DataFrame에서 행을 반복하는 방법

Python은 데이터 중심 Python 패키지의 환상적인 생태계 덕분에 데이터 분석을 수행하는 데 훌륭한 언어입니다. 팬더 이러한 패키지 중 하나이며 데이터를 훨씬 쉽게 가져오고 분석할 수 있습니다.

Pandas에서 행을 반복하는 다양한 방법을 살펴보겠습니다. 데이터프레임 :

방법 1: Dataframe의 index 속성을 사용합니다.

파이썬3




# 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])>

산출:

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 

방법 2: 사용 장소[] 기능 데이터프레임의

파이썬3




# 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'> ])>

산출:

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 

방법 3: 사용 iloc[] 기능 DataFrame의

파이썬3




# 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> ])>

산출:

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 ​ 

방법 4: 사용 반복() 방법 데이터프레임의

파이썬3




# 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'> ])>

산출:

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 

방법 5: 사용 반복() 데이터프레임의 메소드.

파이썬3




# 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'> ))>

산출:

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 ​ 

방법 6: 사용 적용하다() 방법 데이터프레임의

파이썬3




# 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> ))>

산출:

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