[파이썬 코딩]python / numpy / pandas / 연습문제2

in #kr6 years ago

오늘은 pandas에 대한 연습문제를 가져 왔습니다!

numpy를 조금? 활용하는 식도 있구요!

오늘의 연습문제 입니다!

#1. 랜덤으로 5개의정수를 만들어 시리즈로 생성하기
#2. 랜덤으로 5개의 실수를 만들어 시리즈로 생성하기
#3. 혈액형 통계 수치를 이용하여 인덱스를 가지는 시리즈 생성하기

4. 1 ~ 25숫자를 가지는 DataFrame만들기

#5. 아래와 같은 모습의 DataFrame만들기

내가쓴식

1.png
2.png
3.png

설명주석

1#.png
2#.png
3#.png

텍스트 식

#1. 랜덤으로 5개의정수를 만들어 시리즈로 생성하기
#pandas, random 불러오기
import pandas as pd
import random

s = pd.Series([random.randint(1,9) for i in range(5)])
print(s)
#0 9
#1 1
#2 7
#3 5
#4 8
#dtype: int64

#2. 랜덤으로 5개의 실수를 만들어 시리즈로 생성하기
#랜덤으로 실수 값으로 1부터 9까지 랜덤으로 5개 뽑기
s1 = pd.Series([float(random.randint(1,9)) for i in range(5)])
print(s1)
#0 2.0
#1 7.0
#2 8.0
#3 4.0
#4 1.0
#dtype: float64

#3. 혈액형 통계 수치를 이용하여 인덱스를 가지는 시리즈 생성하기
#결과값
#A형 34.2
#B형 27.1
#O형 26.7
#AB형 11.5
#dtype float64

#혈액형
blood = ['A형', 'B형', 'O형', 'AB형']
#혈액형 통계자료
st = [34.2, 27.1, 26.7, 11.5]

s3 = pd.Series(st, index = blood)
#A형 34.2
#B형 27.1
#O형 26.7
#AB형 11.5
#dtype float64

4. 1 ~ 25숫자를 가지는 DataFrame만들기

#numpy 불러오기
import numpy as np

1~25숫자를 5:5형태를 가진 2차원으로 출력

num = np.arange(1,26).reshape(5,5)
#array([[ 1, 2, 3, 4, 5],

[ 6, 7, 8, 9, 10],

[11, 12, 13, 14, 15],

[16, 17, 18, 19, 20],

[21, 22, 23, 24, 25]])

2차원을 데이터프레임으로 만들기

num = pd.DataFrame(num)

#컬럼 설정
num.columns = ['a','b','c','d','e']
#인덱스 설정
num.index = [1,2,3,4,5]
print(num)

a b c d e

#1 1 2 3 4 5
#2 6 7 8 9 10
#3 11 12 13 14 15
#4 16 17 18 19 20
#5 21 22 23 24 25

#5. 아래와 같은 모습의 DataFrame만들기

나이 번호 이름 점수

#0 15 1 a 100
#1 16 2 b 90
#2 15 3 c 80
#3 14 4 d 70

dict형태로 번호,이름,점수,나이 columns값 그리고 각 행의 벨류값 입력

data = [
{'번호':1, '이름':'a', '점수':100, '나이':15},
{'번호':2, '이름':'b', '점수':90, '나이':16},
{'번호':3, '이름':'c', '점수':80, '나이':15},
{'번호':4, '이름':'d', '점수':70, '나이':14}
]

#입력한 데이터를 데이터프레임으로 저장
df = pd.DataFrame(data)
print(df)

나이 번호 이름 점수

0 15 1 a 100

1 16 2 b 90

2 15 3 c 80

3 14 4 d 70

오늘 준비한건 여기까지입니다!

Pandas 맛보기였는데요..!

다음에는 심화를 들고 오도록 하겠습니다 :)

오늘도 읽어주셔서 감사드리고 다들 좋은 하루 되세요!!

Sort:  

실습해보고 싶네요 ㅎㅎ

Congratulations @rexypark! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You got your First payout

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @rexypark! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 10 as payout for your posts. Your next target is to reach a total payout of 50

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!

Thank you so much for sharing this amazing post with us!

Have you heard about Partiko? It’s a really convenient mobile app for Steem! With Partiko, you can easily see what’s going on in the Steem community, make posts and comments (no beneficiary cut forever!), and always stayed connected with your followers via push notification!

Partiko also rewards you with Partiko Points (3000 Partiko Point bonus when you first use it!), and Partiko Points can be converted into Steem tokens. You can earn Partiko Points easily by making posts and comments using Partiko.

We also noticed that your Steem Power is low. We will be very happy to delegate 15 Steem Power to you once you have made a post using Partiko! With more Steem Power, you can make more posts and comments, and earn more rewards!

If that all sounds interesting, you can:

Thank you so much for reading this message!

Congratulations @rexypark! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 63235.53
ETH 2558.76
USDT 1.00
SBD 2.63