Python - Summary 문법 πŸ±β€πŸ

화이티 Β·2024λ…„ 1μ›” 2일
0

Python

λͺ©λ‘ 보기
5/5

Summary_LibraryπŸ‘

πŸ’πŸ’πŸ’PYTHON LIBRARY NUMBY
0.import numpy

  • import numpy as np
  1. Make:
    arr = np.array(list)
    or arr = np.array([[1,2,3],[4,5,6]])
  2. Characteritics
  • Can do sum
    sum(arr1+arr2)
  • count column and row:(arr(row. column))
    arr.shape
  • count which type of array (1,2,or 3)
    arr.ndim
    -count how many indicator inside:
    arr.size
    -to change the datatype:
    arr = np.array(list, dtype = np.int64)
    arr = arr.astype(np.int64)
    -to change the shape:
    arr = arr.reshape(row,column)
    -to range from..to....
    arr = np.arange(startno,tono)
    -to get random no
    np.random.rand(row,column)
    arr= np.random.randint(st,en,size=(row,column))

πŸ“πŸ“πŸ“πŸ“LIBRARY PANDAS

  • import pandas as pd
  1. Make:
    ham 1 chieu: pop = pd.Series([1,2,3,4], index = [de muc], name ='')
    or mang 2 chieu tro len:
    df = pd.DataFrame({key:value})
    =pd.DataFrame(data)
  2. Characteritics
  • Can do sum
    df.sum(axis =1/0)
  • count column and row:(arr(row. column))
    df.shape
  • count which type of array (1,2,or 3)
    df.ndim
    -count how many indicator inside:
    df.size
    -to change the datatype:
    df = arr.astype(np.int64)
    -to change the shape:
    df = df.reshape(row,column)
    -to range from..to....
    df = pd.arange(startno,tono)
    -to get random no
    np.random.rand(row,column)
    arr= pd.random.randint(st,en,size=(row,column))
  • to delete
    del df[' 1']
    df.drop('javascript',inplace = True)
  • to sort value:
    df.sort_values(by = [' ',' '])
    df.sort_values(by = (' ')
  • to sort index
    df.sort_index(ascending = False/True): 인덱슀 κΈ°μ€€μœΌλ‘œ 절렬
  • to import data from excel/csv
    score = pd.read_csv("./data/score.csv", encoding ="cp949", index_col = "κ³Όλͺ©")
  • to add into df
    df.[' '] = [ ]
  • to display(lon nhat toi nho nhat)
    display(score.sort_values(by ='2반')[::-1])
  • to sum in df and add sum as a column
    score['sum']= score.loc[:,:'4반'].sum(axis = 1)
  • to cal min/max
    score.loc["Max score"]=score.loc[:,:'4반'].max(axis =1)
    score.loc["Min score"]=score.loc[:,:'4반'].min(axis =1)
  • to split df
    df['Domain'] = df['Email'].str.split('@').str[1]
  • to use the function
    def assign_grade(score):
    if score>= 80 :
    return 'A'
    elif score >= 60 :
    return 'B'
    elif score >= 40 :
    return 'C'
    elif score >= 20 :
    return 'D'
    else :
    return 'F'
    score.applymap(assign_grade)
  • to merge
    pd.merge(h_info,r_info,how = 'left/right/innner',on = "common thing")
  • to groupby
    age_df.groupby(by=["category"]).count/sum()
  • to concat (merge + arrange order)
    pd.concat([cnt2019.p2019_2020.cnt2020.p2020_2021.cnt2021],axis=1)
  • to rename
    cnt2019.name = "2019 total"

🍏🍏🍏PYTHON_LIBRARY_CHART(MATPLOTLIB)
0. IMPORT
import matplotlib.pyplot as plt
import random
import random as rand
df = pd.DataFrame(np.random.rand(50,4)*100, columns = ['A','B','C','D']).astype(np.int64)

  1. MAKE
    x = [row]
    y = [column]
    (line)
    plt.plot(x,y, ls = '-.',color = 'green', lw = 1, marker = 'βˆ—*',mec = 'green' , mfc = 'green')
    (shape - bar)
    plt.bar(x,y,color = 'lightblue')
    (shape-bar- opposite)
    plt.barh(x,y)
    (scatter)
    plt.scatter(x = df['A'], y = df['B'], label = 'group1',s = 90)
    plt.scatter(x = df['C'], y = df['D'], label = 'group2')
  • to make label
    plt.xlabel('')
    plt.ylabel(' ')
  • to make limited(range)
    plt.lim(begin number, end number)
  • to make title
    plt.title('title name',loc = 'center/left/right')
  • to show the chart
    plt.show()
  • to show data head,tail
    data.head()
    data.tail()
  • to count how many for each element
    data['seach column name'].value_counts()
  • to index the row
    x= y.index
  • to show text in the chart
    for i in range (len(y)):
    plt.text(x[i],y[i],f'{y[i]}')
  • to larger the size
    plt.figure(figsize = (20,10))
  • to sort the largest to smallest
    dt = dt.sort_values('μ‚¬μƒμžμˆ˜', ascending = False)
profile
μ—΄μ‹¬νžˆ κ³΅λΆ€ν•©μ‹œλ‹€! The best is yet to come! πŸ’œ

0개의 λŒ“κΈ€