Posts

Python data beautifulsoup

Beautifulsoup findAll(tag, attributes, recursive, text, limit, keywords) find(tag, attributes, recursive, text, keywords) .findAll({"h1","h2","h3","h4","h5","h6"}) .findAll("span", {"class":"green", "class":"red"}) .findAll(text="the prince") .findAll(id="text") Both are same bsObj.findAll(id="text") bsObj.findAll("", {"id":"text"}) Both are same bsObj.findAll(class_="green") bsObj.findAll("", {"class":"green"}) from urllib.request import urlopen from bs4 import BeautifulSoup html = urlopen("http://www.pythonscraping.com/pages/page3.html") bsObj = BeautifulSoup(html) for child in bsObj.find("table",{"id":"giftList"}).children:     print(child) from urllib.request import urlopen from bs4 import BeautifulSoup html...

Japanese words (will be updated later)

Konnichiwa - Hello sayonara - bye arigato - thank you baka - idiot hai - yes iie - no gommenasai - sorry matane - see you later oyasumi - good night ikou - let's go chaousi wadou - what's up? kudasai - please kampai - bottom's up watashi - I anata - you da me - oh no

Korean words (will be update later)

Shiro - Don't (do that) Kaamchakiya - That surprised me Anyong - Hello ye - yes aanio - no na - I ne - my dangsin - you dangsine - your hwanyong haeyo - welcome butak habnida - Please mian haeyo - sorry goma woyo - thanks gapsida - let's go hapsida - let's do it khambe - bottom's up fighting - for cheering urpose ottoke - how? yojim ottoke jineyo? - How are you? nappun - bad najunge - later najungewoyo - see you later irumin boya? - what's your name? busshun ireyo?  - what happened? yerobun - everyone

Pandas and list compare

import numpy as np import pandas as pd df = pd.DataFrame({'List1':[1,2,3, 4,5,5,11,4],'List 2':[3,5,6,8,9,3,4,9]}, columns=['List1', 'List 2']) #df.to_excel("list1.xlsx", header=True, index=False) df['Intersect']=pd.DataFrame( np.intersect1d(df['List1'], df['List 2'])) #unique common in both df['commonin1']=df['List1'][ np.in1d(df['List1'], df['List 2'])] #non unique common items of list 1 df['commonin2']=df['List 2'][np.in1d(df['List 2'], df['List1'])] #non unique common items of list 2 df['1not2']=pd.DataFrame(np. setdiff1d(df['List1'], df['List 2'])) #in list1 but not in list 2 df['2not1']=pd.DataFrame(np. setdiff1d(df['List 2'], df['List1'])) #in list 2 but not in list1 df['1not2NU']=df['List1'][~np. in1d(df['List1'], df['List 2'])] #in list1 but not in list 2 non unique d...

Pandas snips

https://gist.github.com/gauravmeena0708/78770ccffb0e008cf25b0261e47fb7d8   https://gist.github.com/gauravmeena0708/71a64e34a8e1a0e49927a288cfb3d7a8   https://gist.github.com/gauravmeena0708/4f3e6c1009dec4446ff93ac011be26cf   data = 'a,b,c \n 1,2,3 \n 4,5,6 \n 7,8,9' pd . read_csv ( StringIO ( data ), names = [ 'foo' , 'bar' , 'baz' ], header = 0 )   data = 'skip this skip it \n a,b,c \n 1,2,3 \n 4,5,6 \n 7,8,9' pd . read_csv ( StringIO ( data ), header = 1 )   pd . read_csv ( StringIO ( data ), usecols = [ 'b' , 'c' ]) pd . read_csv ( StringIO ( data ), usecols = [ 0 , 2 , 3 ]) pd . read_csv ( StringIO ( data ), usecols = lambda x : x not in [ 'a' , 'c' ])     data = ' \n a,b,c \n \n # commented line \n 1,2,3 \n\n 4,5,6' pd . read_csv ( StringIO ( data ), comment = '#' )       data = 'a,b,c \n\n 1,2,3 \n\n\n 4,5,6' pd . read_csv ( StringIO ...

Batch rename and xls to csv

''' #Remove spaces from filename import os path = 'C:\\Users\\gaura\\Downloads\\estmst' #os.getcwd() filenames = os.listdir() for filename in filenames: os.rename(os.path.join(path, filename), os.path.join(path, filename.replace(' ', '-'))) ''' import os , pandas as pd path = 'C: \\ Users \\ gaura \\ Downloads \\ estmst' for filename in os . listdir(path): data_xls = pd . read_excel(filename, 'Main Dashboard Excel' , index_col = None ) data_xls . to_csv(filename + '.csv' , encoding = 'utf-8' , index = False )

Merge multiple xls files

Compiled from various sources on internet 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import os , pandas as pd # filenames excel_names = [] for filename in os . listdir( '<Dir path' ): if filename . endswith( '.xls' ): excel_names . append(filename) excels = [] for name in excel_names: try : excels . append(pd . ExcelFile(os . path . join( ' ' , name))) except IndexError : print (name) # turn them into dataframes frames = [x . parse(x . sheet_names[ 0 ], header = None ,index_col = None ) for x in excels] # delete the first row for all frames except the first # i.e. remove the header row -- assumes it's the first frames[ 1 :] = [df[ 1 :] for df in frames[ 1 :]] # concatenate them.. combined = pd . concat(frames) # write it out combined . to_excel( "c.xlsx" , header = False , index = False )