Pworkspace

week13 - pandas(2), 과제5, 기말고사 공지(?)

haerangssa 2024. 5. 28. 12:44

head(), tail() 메소드의 적용

ser = pd.Series(np.random.randn(1000))

print(ser.head())
print(ser.tail(3))

ind = pd.date_range('1/1/2019', periods=5)
ser = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
df = pd.DataFrame(np.random.randn(5, 3), index=ind, columns=['A', 'B', 'C'])
print(df[:2])

df = pd.DataFrame({'one':pd.Series(np.random.randn(2),index=['a','b']),
                                                   'two':pd.Series(np.random.randn(3),index=['a','b','c']),
                                                   'three':pd.Series(np.random.randn(2),index=['b','c'])})
df
df.iloc[1]
df['two']
row = df.iloc[1] #데이터 프레임의 행이나 칼럼의 순서를 나타내는 정수로 특정 값을 추출해오는 방법
col = df['two'] #two에 해당하는 col 데이터들이 빠져나옴

rint(df.sub(row, axis='columns'))
print(df.sub(col, axis=0))


#fill_value=0, 손실값의 대치
print(df)
print(df1)
print(df.sub(df1, fill_value=0))

#아래중요 뭔데 시발
print(df.mean())
print(df.sum())
print(df.std())
/usr/local/bin/python3.9 /Users/haerangssa/Desktop/advancedpy/week13/week13.py 
--------------------------
0   -0.067541
1    0.568346
2   -0.086985
3   -0.587609
4    0.662903
dtype: float64
997   -0.381092
998    0.674514
999    1.760487
dtype: float64
                   A         B         C
2019-01-01 -0.488729  0.991840 -1.896369
2019-01-02  0.512930 -1.640738 -0.947173
        one       two     three
a  0.412928  1.247372       NaN
b  0.000000  0.000000  0.000000
c       NaN -0.683719 -0.811483
        one  two     three
a -2.519117  0.0       NaN
b -1.684674  0.0 -0.391588
c       NaN  0.0 -0.519352
        one       two     three
a -0.764070  1.755048       NaN
b -1.176998  0.507676  0.116088
c       NaN -0.176043 -0.695395
        one       two     three
a       NaN -0.893819  1.288062
b  0.574440  1.097481  0.294602
c -0.804302  0.000672       NaN
        one       two     three
a -0.764070  2.648867 -1.288062
b -1.751438 -0.589805 -0.178514
c  0.804302 -0.176715 -0.695395
one     -0.970534
two      0.695560
three   -0.289653
dtype: float64
one     -1.941068
two      2.086680
three   -0.579307
dtype: float64
one      0.291984
two      0.979160
three    0.573805
dtype: float64

Process finished with exit code 0

 

데이터를 더할 때 na겂둘운 0ㅇ로 취급
대이ㅓ가 모두 na이면 결과는 0
skipna 옵션
std()

object는 count unique top freq 만 출력
all은 전부 출력'''
ser = pd.Series(['a','a','b','c','c',np.nan, 'c', 'd'])

df = pd.DataFrame({'a': ['Yes', 'Yes', 'No', 'No'], 'b': range(4)})

print(df.describe())
print(df.describe(include=['object']))
print(df.describe(include='all'))

df = pd.DataFrame(np.random.randn(4, 3), columns=['A', 'B', 'C'])
print(df)
print(df.idxmin(axis=0))
print(df.idxmin())
print(df.idxmax(axis=1))

중요! series.nunique() - 내가 가진 숫자 중 유니크한 벨류 몇개 았느냐

describe() - 백분위수 선택
describe()를 문자로 하는 경우
object - count unique top freq 만 출력

all - 전부 출력

 

idxmax. idxmin : 최대값 최소값 요소의 인덱스 계산

#value_counts() 중요

#output
/usr/local/bin/python3.9 /Users/haerangssa/Desktop/advancedpy/week13/week13.py 
              b
count  4.000000
mean   1.500000
std    1.290994
min    0.000000
25%    0.750000
50%    1.500000
75%    2.250000
max    3.000000
          a
count     4
unique    2
top     Yes
freq      2
          a         b
count     4  4.000000
unique    2       NaN
top     Yes       NaN
freq      2       NaN
mean    NaN  1.500000
std     NaN  1.290994
min     NaN  0.000000
25%     NaN  0.750000
50%     NaN  1.500000
75%     NaN  2.250000
max     NaN  3.000000
          A         B         C
0  0.205580 -0.519514  0.965999
1  0.402899  0.407342  0.630724
2  0.045062  0.638130 -1.767252
3 -0.228875  0.196585  0.503170
A    3
B    0
C    2
dtype: int64
A    3
B    0
C    2
dtype: int64
0    C
1    C
2    B
3    C
dtype: object

Process finished with exit code 0

 

 

print("----------------------")
data=np.random.randint(0,7,size=30)
ser1=pd.Series(data)
print(data)
print(ser1.value_counts()) #뭐랬더라? 많은 순으로 sorting함
# #??
#output
----------------------
[4 0 0 3 0 2 5 6 5 2 3 5 3 6 2 0 0 3 6 6 5 3 6 5 5 5 4 3 1 1]
5    7
3    6
0    5
6    5
2    3
4    2
1    2

 

 

총 출력개수

1. 1000개를 랜덤하게 설정한거를 도출값 다 출력하고 

2. 2000개를 랜덤 ~

해서 총 150개 출력????

걍 프린트만 하면 됨. 

 

슬라이싱방식은 안됨(0부터 잘라서 선택)- 샘플 랜덤하게.

 

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.quantile.html

 

 

pandas.DataFrame.quantile — pandas 2.2.2 documentation

Include only float, int or boolean data. Changed in version 2.0.0: The default value of numeric_only is now False.

pandas.pydata.org

 

기말고사

1. 오픈북

2. 종속형 문제

3. 문제 개수는 달라질 수 있음 max7개

4. figure출력 많음

5. 채점 빡빡함

씹알