상세 컨텐츠

본문 제목

판다스, 데이터프레임 다루기

판다스 입문

by ZISEN 2021. 3. 20. 18:58

본문

반응형

불린 추출과 브로드캐스팅

1.불린 추출하기

데이터프레임도 불린 추출을 할 수 있습니다. 다음은 데이터프레임의 Age 열에서 Age 열의 평균보다 높은 행만 출력합니다.

print(scientists[scientists['Age'] > scientists['Age'].mean()])

                   Name        Born        Died  Age     Occupation
1        William Gosset  1876-06-13  1937-10-16   61   Statistician
2  Florence Nightingale  1820-05-12  1910-08-13   90          Nurse
3           Marie Curie  1867-11-07  1934-07-04   66        Chemist
7          Johann Gauss  1777-04-30  1855-02-23   77  Mathematician

 

2. 브로드캐스팅하기

데이터프레임에 스칼라 연산을 적용하면 어떻게 될까요? 앞에서 시리즈에 스칼라 연산을 적용할 때는

모든 요소에 스칼라를 적용하여 연산했습니다. 데이터프레임도 마찬가지입니다.

scientists 데이터프레임에 2를 곱하면 정수 데이터는 2를 곱한 숫자가 되고 문자열 데이터는

문자열이 2배로 늘어납니다.

print(scientists * 2)


                                       Name                  Born  \
0        Rosaline FranklinRosaline Franklin  1920-07-251920-07-25   
1              William GossetWilliam Gosset  1876-06-131876-06-13   
2  Florence NightingaleFlorence Nightingale  1820-05-121820-05-12   
3                    Marie CurieMarie Curie  1867-11-071867-11-07   
4                Rachel CarsonRachel Carson  1907-05-271907-05-27   
5                        John SnowJohn Snow  1813-03-151813-03-15   
6                    Alan TuringAlan Turing  1912-06-231912-06-23   
7                  Johann GaussJohann Gauss  1777-04-301777-04-30   

                   Died  Age                            Occupation  
0  1958-04-161958-04-16   74                        ChemistChemist  
1  1937-10-161937-10-16  122              StatisticianStatistician  
2  1910-08-131910-08-13  180                            NurseNurse  
3  1934-07-041934-07-04  132                        ChemistChemist  
4  1964-04-141964-04-14  112                    BiologistBiologist  
5  1858-06-161858-06-16   90                    PhysicianPhysician  
6  1954-06-071954-06-07   82  Computer ScientistComputer Scientist  
7  1855-02-231855-02-23  154            MathematicianMathematician  

 

출처 : doit 데이터 분석을 위한 판다스 입문

반응형

관련글 더보기