from pandas import DataFrame as f import numpy as np import random item = ['Onion', 'Sugar', 'Potato', 'Carrot'] * 5 place = ['Delhi'] * 4 + ['Bhopal'] * 4 + ['Bangalore'] * 4 + [ 'Kolkata' ] * 4 + ['Mumbai'] * 4 total = random.sample(range(10, 50), 20) df = f({'Item': item, 'Place': place, 'Total': total}) print df, "\n\n" while True: x = input( "\n1.Display Itemwise Rank \n2.Display Itemwise Rank \n3.Exit\n>") if x == 1: it = raw_input("Enter the item Name : ") il = df[df['Item'] == it] il = il.copy() tol = il['Total'] rank = tol.rank(ascending=False) il['Rank'] = rank print il elif x == 2: p = raw_input("Enter the place Name : ") pl = df[df['Place'] == p] pl = pl.copy() tol = pl['Total'] rank = tol.rank(ascending=False) pl['Rank'] = rank print pl else: break
#!/usr/bin/python from pandas import Series as s from pandas import DataFrame as f import pandas as pd import numpy as np from matplotlib import pyplot as plt fsales = f(np.random.randint(1, 100, 20).reshape(5, 4)) fsales.index = 'apple orange grapes mango banana'.split() fsales.columns = 'Jan Feb Mar Apr'.split() so = fsales.plot(kind='bar', rot=45) print fsales plt.show()
import pandas as pd from pandas import DataFrame as f c = pd.read_csv('emp.csv') ef1 = f(c) ef1 a = ef1['ephno'] ef2 = f(a) ef1 = ef1[['ename', 'eno', 'edesig', 'esalary']] ef1
import pandas as pd import numpy as np from pandas import DataFrame as f from pandas import Series as s #Question 1 Create a (4,4)2D array consisting of integers. a = np.array(range(1, 17)).reshape(4, 4) #Question 2 Create a masked array from the above array by making numbers which are divisible by 3 as invalid values. mska = np.ma.masked_array(a, mask=a % 3 == 0) print mska.mask, "\n" #Question 3 Create a dataframe from the above masked array. dfma = f(mska) print dfma, "\n" #Question 4 Create a series of your own choice and do reindexing by exploring ffill,bfill,nearest etc s1 = s(range(4), index=list("abcd")) print s1, "\n" print s1.reindex(list("abcde")), "\n" print s1.reindex(list("abcde"), method="ffill"), "\n" print s1.reindex(list("abcde"), method="bfill"), "\n" #print s1.reindex(list("abecd"),method="nearest"),"\n" #Question 5 Create a Dataframe object of your own choice and do reindexing. ad = np.arange(1, 10).reshape(3, 3) df1 = f(ad, columns=list("abc"), index=list("xyz")) print df1, "\n" print df1.reindex(list("yzx")), "\n" #Question 6 Create a dataframe object from the above array by making c1,c2,c3,c4 as column indices and r1,r2,r3,r4 as row indices.
print(b) ''' #-------------------------------------------------- import numpy as np import pandas as pd from pandas import DataFrame as f global d d = {'Item':['a','b','c','e'],\ 'Place':['MH','KL','JK','TN'],\ 'Total sale':[100,200,500,300]} global df df = f(d) def ItemWise(): print(df) p = input('enter a item name :') for i, j in zip(df.index, df['Item']): if j == p: print('Place Wise rank of ', p, ' is ', df.rank()['Place'][i]) def placeWise(): print(df) q = input('enter a place name: ')
import pandas as pd from pandas import DataFrame as f import numpy as np df = f(np.random.randint(10, 20, 40).reshape(10, 4), columns="c1 c2 c3 c4".split()) print df print df.sort_values(['c1', 'c3'], ascending=[True, False])