You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As seen above, now the index is a list of integers (int64)
Get index at location 3 (zero based)
df2.index[3]
4
Get the row where index is 3:
df2.loc[3]
A 31
B 32
Name: 3, dtype: int64
df2
A
B
i
1
11
12
2
21
22
3
31
32
4
41
42
5
51
52
Get the n-th row:
n=3df2.iloc[n]
A 41
B 42
Name: 4, dtype: int64
# or df2[n:n+1]
A
B
i
4
41
42
The difference between df2.iloc[n] and df2[n:n+1] is, iloc always returns a Series, while the other returns a DataFrame.
My Helpers
These helpers will make your life easy
defdf_col(self, column) ->pd.Series:
iftype(column) ==int:
returnself[self.columns[column]].copy()
iftype(column) ==str:
returnself[column].copy()
raiseValueError(f"col parameter must be either type of 'str' or type of 'int' which '{column}' is not")
defdf_cols(self, *columns) ->pd.DataFrame:
cols_list=list(columns)
new_cols_list= []
forcincols_list:
iftype(c) ==int:
new_cols_list.append(self.columns[c])
else:
new_cols_list.append(c)
returnself[new_cols_list].copy()
defdf_row(self, ix) ->pd.Series:
returnself.iloc[ix].copy()
defser_col(self, column):
returnself.get(column)
defser_cols(self, *columns):
result= []
forcinlist(columns):
result.append(self.get(c))
returntuple(result)
defser_row(self, ix):
returnself.values[ix]
pd.DataFrame.col=df_colpd.DataFrame.cols=df_colspd.DataFrame.row=df_rowpd.DataFrame.rows=pd.DataFrame.ilocpd.Series.col=pd.Series.getpd.Series.cols=ser_colspd.Series.row=ser_rowpd.Series.rows=pd.Series.values
Single Column Helper (col):
Returns Series
df2.col(0) # Get first column as Seriesdf2.col('A') # Get column 'A' as Series
i
1 11
2 21
3 31
4 41
5 51
Name: A, dtype: int64
Multiple Columns Helper (cols):
Returns DataFrame
df2.cols(0,1) # Get column first and second as DataFramedf2.cols('A', 'B') # Get column 'A' and 'B' as DataFrame
A
B
i
1
11
12
2
21
22
3
31
32
4
41
42
5
51
52
Single Row Helper (row):
Returns Series
df2.row(0) # Get first row as Series
A 11
B 12
Name: 1, dtype: int64
Multiple Rows Helper (rows):
Returns DataFrame
df2.rows[0:3] # Get rows from 0 to 3 (excluding 3) as DataFrame
A
B
i
1
11
12
2
21
22
3
31
32
Mixing Row and Column Helpers:
Single column of single row -> raw value
df2.row(0).col(0) # Get first column of first row as valuedf2.row(0).col('A') # Get column 'A' of first row as value
11
Multiple column of single row -> tuple:
df2.row(0).cols(0, 1) # Get first two columns of first row as tupledf2.row(0).cols('A', 'B') # Get 'A' and 'B' columns of first row as tuple
(11, 12)
Single column of multiple rows -> Series
df2.rows[0:3].col(0) # Get first column of rows from 0 to 3 (excluding 3) as Seriesdf2.rows[0:3].col('A') # Get column 'A' of rows from 0 to 3 (excluding 3) as Series
i
1 11
2 21
3 31
Name: A, dtype: int64
Multiple column of multiple rows -> DataFrame
df2.rows[0:3].cols(0, 1) # Get first two column of rows from 0 to 3 (excluding 3) as DataFramedf2.rows[0:3].cols('A', 'B') # Get 'A' and 'B' columns of rows from 0 to 3 (excluding 3) as DataFrame
A
B
i
1
11
12
2
21
22
3
31
32
Single row of single column -> raw value:
df2.col(0).row(0) # Get first row of first column as valuedf2.col('A').row(0) # Get first row of column 'A' as value
11
Multiple rows of single column -> numpy.ndarray:
df2.col(0).rows[0:3] # Get rows from 0 to 3(excluding) of first column as numpy.ndarraydf2.col('A').rows[0:3] # Get rows from 0 to 3(excluding) of column 'A' as numpy.ndarray
array([11, 21, 31])
Single row of multiple columns -> Series:
df2.cols(0, 1).row(0) # Get first row of first two columns as Seriesdf2.cols('A', 'B').row(0) # Get first row of columns 'A' and 'B' as Series
A 11
B 12
Name: 1, dtype: int64
Multiple rows of multiple columns -> DataFrame:
df.cols(0, 1).rows[0:3] # Get rows from 0 to 3(excluding) of first two columns as DataFramedf.cols('A', 'B').rows[0:3] # Get rows from 0 to 3(excluding) of of columns 'A' and 'B' as DataFrame