引き続きUdemyでPython for Time Series Data Analysisで、pandasの基礎の学習を進める。pandasライブラリの便利な関数を使えば、簡単にdataの整形ができそうだ。
pandasの学習コード4つ目
import pandas as pd data = {'Company':['GOOG','GOOG','MSFT','MSFT','FB','FB'],'Person':['Sam','Charlie','Amy','Vanessa','Carl','Sarah'],'Sales':[200,120,340,124,243,350]} df = pd.DataFrame(data) df df.groupby('Company').mean() df.groupby('Company').describe().transpose()
pandasの学習コード5つ目
import pandas as pd df = pd.DataFrame({'col1':[1,2,3,4],'col2':[444,555,666,444],'col3':['abc','def','ghi','xyz']}) df.head() len(df['col2'].unique()) df['col2'].unique() df['col2'].value_counts() newdf = df[(df['col1'] > 2) & (df['col2'] == 444)] newdf def times_two(number): return number*2 df['new'] = df['col1'].apply(times_two) del df['new'] df df.columns df.index df.info() df.describe() df df.sort_values('col2',ascending=False)
pandasの学習コード6つ目
import pandas as pd pd.read_csv('example.csv') df newdf = df[['a','b']] newdf.to_csv('mynew.csv',index=True) pd.read_excel('Excel_Sample.xlsx',sheet_name='Sheet1') df.columns df.drop('Unnamed: 0',axis=1) mylist_of_tables = pd.read_html('https://www.fdic.gov/bank/individual/failed/banklist.html') type(mylist_of_tables) len(mylist_of_tables) df = mylist_of_tables[0]
pandasの学習コード7つ目
import pandas as pd pd.read_csv('example.csv') df newdf = df[['a','b']] newdf.to_csv('mynew.csv',index=True) pd.read_excel('Excel_Sample.xlsx',sheet_name='Sheet1') df.columns df.drop('Unnamed: 0',axis=1) mylist_of_tables = pd.read_html('https://www.fdic.gov/bank/individual/failed/banklist.html') type(mylist_of_tables) len(mylist_of_tables) df = mylist_of_tables[0]