0% found this document useful (0 votes)
39 views

DataFrame Notes

Uploaded by

twinntower.9.11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
39 views

DataFrame Notes

Uploaded by

twinntower.9.11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 12
What is dataframe? It is 2D (Two Dimensional) data structure. Used to manage large and complex data in tabular format It contains both rows and columns and hence have both row and column indexes * Most commonly used data structure similar to spreadsheet. Features of dataframe # Itcan store any type (heterogeneous) of data © Itis size mutable © Itis value mutable * Both indexes can be labelled * Indexes may constitute any type of value such as number, string, character, Boolean value * Index of dataframe can also be referred as ‘axis = O refers to row index and axis = 1 refers to column index Creating dataframe While creating dataframe we should remember following points. © We must include python pandes library in our program. * Dataframe() method of pandas library is used to create datattame. © Dataframe can accept data from © List * Dictionary © Tuple © string «Series «Another dataframe © Numpy array Syntax for creating dataframe Import pandas as = .DataFrame(data, index, columns, dtype) in the above syntax. arguments we us # Data— Values to be in dataframe. It can be any collection such as lst, nparray, dictionary abel index for rows. Itis optional, and if no an numbers from 0 to n-1 is 0 label index for columns. It is optional, and if not passed than numbers from 0 to 0 each column, * Dtype - used to define data type for columns, jon and if not defined, None is appli ~ Index and colurnn are positional independent — The no of index values to be passed in index sequence must maich the length of data to be passed Exp-1: Dataframe with default indexes ry = [23,34,45,54] ESE = Index values will be generated automatically Exp-2: Dataframe with labelled Indexes enter Pee ECE CTY) Po coer Poe ec tess stars ple", banana"), column CR er ener aa) acta} Creating Dataframe u ‘© We can create dataframe by passing list as data © ifthe 1s of different length is passed in dataframe , than NaN or missing value is assigned to its corresponding column, Exp-3: Create Dataframe using list with default indexes Bees cae CER ad apple = [10,20, 30,40) banana = [23,34, 45,54] Cae eclac des Scr CUccceD) print (df) Exp-4 Create Dataframe using list with labelled indexes Perea T apple = [10,20,30,40] proc Mea CeTe Lao] Caer c UNC s sea CU LCSne Eis (cee STP SB) Peseta) Exp-5: Create Dataframe using multiple list of different length ce ost eanet OO} ev aE ECE] aoe Cee ssa CeCe sneer eS een se} SES to) — NaN is automatically inserted if no matching value is found for columns. ~ The length of index value being passed must be equal to length of column and row with largest length. ~ If length of index and length of column/row does not match, value error is generated © Wee ‘© Dataframe is 2 rep jumns, it forms a dataframe, © When different Series obj ows ar eset Tey) print (df) Exp-7: Create dataframe passing single Series object within list (as list item) pe raae ce El Penn std OSC Ueee Eee oe oe OCs era) Precenrertse eee) Saetac foros Exp-8: Create dataframe passing multiple Series object Sere UC Cee ! eee est eee eC as eC eee te) ee Ose e CIS CIES EReC CIs CUE aE ta Seon ese eRe Sd BE Sk See pesexa ta) Exp-9: Create dataframe passing multiple Series object (with different set of index labels) Brrr cae CRT ST Peet ds URES PREE Ce Rete ooU Poe Ro tsC Ge CeCe PUL ae SoC tee SESS Cac ee Print (df) Creating Dataframe using Dictionary ‘* Dictionary can also be passed as data to create dataframe ‘By default, keys of dictionary are taken as column labels of dataframe ‘Values of dictionary are taken as input data of dataframe ‘* We can specify our own index ’s for dataframe using dictionary as input data Create dataframe passing dictionary with scalar (single) value peraraen Cod Coarse a Serre) eo eCp) print (df) Co Exp-11: Create dataframe passing dictionary having values as list coon eee ace eR ET LTE LIELCSBY Crete enc print (df) fora: Exp-12: Create dataframe passing dictionary of list with own index peter eaters ceed Ral ee Cea PTSD eX res CULISCZeI Ey Caen sc Care oeaRe CN act poster) Cd PCN Perea Exp-13: Create a dataframe passing list of Series ceeeeecec eee Cope Mestre Tt Ree CS CAPE ST) ee ett ae CoD) Coan eae entry Pree eteteoonC) print (dé) Co Pere ete a Si vehas SEM Exp-14: Create a dataframe passing list of Series ccc CRC! Fenesc as oe Lt ec SU SGRS ott TRPR CO. MED CER 2 VERS BEt-C ro Iees Pee <1) P = pd.Series({7, 12,6], index=["r1',"r2", "r3']) Cara aet eee Cries) Cree ett ec onic) creat ee eters eee Shar vee Exp-15: Create a dataframe passing list of Series of different length with different labeled indexes Peete esc Peaster crt ee ce CO en ge SRR CYC CE RBEECT od Bate to. h 275) see eC eC SECC Coe RPA) Ce See sec reece ict PeseraGray oa Pee ces Pees SESeRsSSED Eee St Res ~ The resulting row labels or indexes are union of all indexes of Series used to create dataframe. = Every column in dataframe is a series. — NaN value is automatically inserted at the missing place, ig dataframe u: jonary) g dictionary of dictionary (nested © We can also ci bj +t using 2D dictio y havin: The keys of inner dictionaries make the indexes or row labels and keys of outer dictionaries make the column labels. * IFinner dictionaries have non matching keys, then resulting indexes of dataframe are union of all inner keys. * Ifa key has no matching key in other dictionaries, then NaN is automatically inserted at missing place Exp-16: creating dataframe using 2d dictionary having values as dictionary caer Cee eae arn eee ere Rae eee ed Se eee ets ee UES RCS eC Crapo erties tl) df) Coens coeeeeed peered Cores are ce Coects te cc arr sey Exp-17: creating dataframe u keys 9 2d dictionary having values as dictionary with non-matching poe a eee Ronee este Me Sts Macc eto AE eC ULES PC EVaPRE eet act setey Seer sseticl) peer) Corer See Pere Coe a ae! Peoccey Center Peper eS sEy Creating dataframe using numpy Array Exp-17: eCUEPET Pee Cee Cees noone 2 Pa eo OED) nt (df) Caos ing dataframe from another Dataframe object cee epee err TePer ae er eSE RS Petr eo) eerste cee Pett arse ac) Cece e Scots) rae toto) Brae ee eee uC rs) print (df_dup) Dataframe Attributes Attribute refers to properties of dataframe. Using datafran attribute we can get all kind of inf related to it. Following table list all dataframe attributes: Attributes | Description index Returns row labels of dataframe Columns Returns the column labels of detaframe Axes Returns both row and column indexes size Returns total no of elements of dataframe including missing values Shape Returns total no of rows and columns of datatrame as tuple Values Return dataframe as numpy array Empty Returns true if dataframe is empty T Transpose dataframe's index and columns

You might also like