import pandas as pd pd.__version__ print(help(pd)) dir(pd) import pandas as pd; import numpy as np pd.Series([1, 2, 3, 4, 5]) pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) pd.Series(np.array([1, 2, 3, 4, 5])) pd.Series(np.array([1, 2, 3, 4, 5])) + pd.Series(np.array([5, 4, 3, 2, 1])) d = {"b": 1, "a": 0, "c": 2}; pd.Series(d) df = pd.DataFrame(np.random.randn(5,4),index='A B C D E'.split(),columns='W X Y Z'.split()) df import pandas as pd d = {"one": pd.Series([1.0, 2.0, 3.0], index=["a", "b", "c"]), "two": pd.Series([1.0, 2.0, 3.0, 4.0], index=["a", "b", "c", "d"]) } df = pd.DataFrame(d); print(type(df)); print('\n'); df.info(); print('\n'); df import pandas as pd import numpy as np df=pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8], [9, 20, 22, 34]], index=[0,1,2], columns=['A', 'B', 'C', 'D']) df # source: Yahoo Finance stock = pd.DataFrame([['tech', 'AAPL', 157.25, 1.23, 26.11, 2539, 0.58, 184.01], ['tech', 'MSFT', 261.47, 0.93, 26.98, 1942, 0.95, 333.00], ['engr', 'XOM', 93.87, 1.08, 10.27, 391, 3.68, 103.28], ['engr', 'COP', 109.45, 1.38, 8.87, 137, 1,75], ['fin', 'BAC',33.47, 1.40, 10.46, 269, 2.62, 42.41], ['fin', 'JPM',114.51, 1.10, 9.18, 336, 3.52, 138.80], ['fin', 'WMT',335.50, 1.39, 7.53, 114, 3.00, 394.07]]) stock stock.columns=['sector', 'ticker', 'price', 'beta', 'pe', 'mktcap', 'dy', 'target'] stock.index = ["a", "b", "c", "d", "e", "f", "g"] stock stock.info() stock.index = [0, 1, 2, 3, 4, 5, 6] stock stock.drop('target',axis=1) # but this wont affect the orginal data stock # must put down inplace option stock.drop('target', axis=1, inplace=True) stock stock['target'] = [184.01, 333.00, 103.28, 75.00, 42.41, 138.80, 394.07] stock stock2 = stock.drop(6, axis=0) stock2 newstock = pd.DataFrame([['ind', 184.71, 1.00, 14.61, 98, 2.63, 214.00]], index=['CAT'], columns=['sector', 'price', 'beta', 'pe', 'mktcap', 'dy', 'target']) newstock stock3 = stock.append(newstock) stock3 stock3 = pd.concat([stock, newstock]) stock3 stock.loc[6, 'ticker'] = 'GS' stock stock['price'].replace(335.50, 435.50, inplace=True) stock print(stock['ticker']); print('\n'); print(stock[['ticker']]); print('\n'); print(stock.ticker); print('\n'); print(stock.loc[:, 'ticker']); print('\n'); print(stock.iloc[:, 1]); print('\n'); print(stock.iloc[:, -1]); print('\n'); stock.loc[:, 'ticker':'beta'] stock.loc[:, :] stock.loc[0:2, 'beta'] stock.loc[3, "ticker"] stock.loc[5, "mktcap"] > stock.loc[6, "mktcap"] stock.index stock.describe() print(stock['price'].mean()); print(stock['beta'].median()); print(stock['dy'].std()); print(stock['mktcap'].max()); stock.groupby('sector').mean().round(2) stock.set_index('ticker', inplace=True) stock.loc['JPM', 'mktcap'] > stock.loc['GS', 'mktcap'] stock