Learn_pd_and_dataframe
学校のデータ分析の授業でやっとpandasやdataframeの基礎を学んだのでまとめてきたいと思う。これでkaggleもできるようになると思うよ!! chapter 1 importing import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns import sklearn as sl reading csv salary = pd.read_csv('salary.csv') dataframe meta operation # print first 5 rows df.head() # print first n rows df.head(n) # print shape print(df.shape) # print columns print(df.columns) # print info print(df.info()) converting dataframe to list or numpy # convert dataframe's columns to string list string_list = df.columns.values getting specific column game_name = df['game_name'] getting dummiy of specific column (prefix is optional) dummy_column = pd.get_dummies(df['specific_row'],prefix='pref') slice specific row and column # get slice of rows from 100 to 199 slieced = df.iloc[100:200,:] # get specific columns all_scores = ['Critic_Score', 'User_Score'] reg_sales_df = game_sales[all_scores] # get specific clumns like specified all_sales = game_sales.filter(like='Sales') # get specific rows and columns sample_info = eng_salary.loc[:11244,['Degree', 'Specialization', 'Salary']] chapter 2 (Filtering) filter by condition # get rows where Year_of_Release is 2016 filter = game_sales['Year_of_Release'] == 2016 year_of_2016 = game_sales[filter] # get rows which satisifes the multiple condition and print specific columns pub_is_Nintendo = game_sales['Publisher'] == 'Nintendo' year_is_85_to_95 = (game_sales['Year_of_Release'] >= 1985) &\ (game_sales['Year_of_Release'] <= 1995) Nintendo_85_to_95 = game_sales[year_is_85_to_95 & pub_is_Nintendo] print(Nintendo_85_to_95[['Name', 'Publisher', 'Year_of_Release']], end='\n\n') # same as above but in one line. sp_omit = 'information technology' cond1 = eng_salary['Specialization'] != sp_omit cond2 = eng_salary['English'] <= 400 cond = cond1 & cond2 output = eng_salary.loc[cond,['Specialization','English']] grouping (IMPORTANT) まずはgroupingがどんなことをするのかだけど、ある列について、同じものをまとめるって感じだな。そして、その列についてまあ、色々と操作をするわけです。例えば各出版社ごとの売上とかもかんたんに計算できるようになるってことです。これはクッソ便利ですよね??はい、マジで便利なんです。 ...