13. Python - Pandas - Indexing/Selecting/Assigning
Selecting Columns:
data_frame.column_name // use a method way
data_frame['column_name'] // indexing way
Selecting Data In Columns:
data_frame['column_name'][indexnumber] // indexing way
Selecting with 'iloc' and 'loc' :
Selecting a Column/Row with iloc:
data_frame.iloc[rowindex]
data_frame.iloc[ rowstart_index:rowend_index , column index]
// index can be negative which means start from the end
data_frame.iloc[[1,2,3],0]
// select a list of specified indexes in first column
// colon species all. if use colon instead of '0' then it selects all columns
Selecting a Column/Row with loc:
data_frame.loc[rowstart_index:rowend_index, ['COLUMNA','COLUMNB']]
// loc uses a list of column to select
Selecting using Conditions:
data_frame.loc[data_frame.COLUMN == 'Value']
// selects only in COLUMN that has that 'Value'
data_frame.loc[(data_frame.COLUMNA == 'Value') | (data_frame.COLUMNB >= 50)]
// two conditions. Use '&' or '|'
data_frame.loc[data_frame.COLUMN.isin(['VALUEA' , 'VALUEB'])]
// lets use 'isin' instead of conditional symbols. provide a list of values as condition to check if in
column and then select those column/rows
data_frame.loc[data_frame.COLUMN.notnull()]
data_frame.loc[data_frame.COLUMN.isnull()]
// using 'notnull()' or 'isnull()' to select null rows
Setting Custom Index:
data_frame.set_index("COLUMNNAME_INDEXTOUSE")
Assigning Values:
data_frame['COLUMNA'] = 'FOOT'
// this is constant assigning and makes every value in COLUMNA equal to 'FOOT'
Iterable Values:
data_frame['COLUMNA'] = range(len(data_frame),0,-1)
// loops from length of data_frame to '0' by decrementing 1 and assigning it as the value each loop
Comments
Post a Comment