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
Drop the ‘QUANTITY’ from the data frame df, Here axis = 1 is for the column.
df2 = df.drop([1, 3], axis=0)
Drop rows.
Drop 2nd and 4th rows of data frame df, Here axis = 0 is for row
DataFrame Retrieving Series/DataFrame Information and Slicing
Action
Command
Description
Observation
df.head()
Print the first 5 rows.
df.tail()
Print the last 5 rows.
df.sample(n)
Select randomly n rows from the DataFrame.
Selection Column Data
df['FRUITS']
Select a single column value with the name of the column.
df[['FRUITS', 'PRICE']]
Select more than one column with its name.
df.filter(regex='F | Q')
Select the column whose names match the patterns of the respective regular expression i.e ‘FRUITS’ & ‘QUANTITY’
Getting Subsets of Rows or Columns
df.loc[:, 'FRUITS':'PRICE']
Select all the columns between 'Fruits' and 'Price'.
df.loc[df['PRICE'] < 70, ['FRUITS', 'PRICE']]
Select 'FRUITS' name having 'PRICE' < 70.
df.iloc[2:5]
Select 2 to 5 rows.
df.iloc[:, [0, 2]]
Select the columns having 0th & 2nd positions.
df.at[1, 'PRICE']
Select a single 'PRICE' value at the 2nd row of the 'PRICE' column.
df.iat[1, 2]
Select the single values by their position.
Filter
df.filter(items=['FRUITS', 'PRICE'])
Filter by column name. Select the ‘FRUITS’ and ‘PRICE’ column of the data frame
df.filter(items=[3], axis=0)
Filter by row index. Select the 3rd row of the data frame. Here axis = 0 is for row
df['PRICE'].where(df['PRICE'] > 50)
Returns a new Series object with the same length as the original 'PRICE' column. Replaces values where the condition is False with NaN or another specified value.
df.query('PRICE > 70')
Filter a DataFrame based on a specified condition. Return the rows having PRICE > 70.
Combine Two Data Sets
Action
Command
Description
Merge Two DataFrames
pd.merge(df1, df2, how='left', on='Fruits')
Left Join: Merge the two data frames df1 and df2 based on the ‘Fruits’ column of the left data frame i.e df1
pd.merge(df1, df2, how='right', on='Fruits')
Right Join: Merge the two data frames df1 and df2 based on the ‘Fruits’ column of the right data frame i.e df2
pd.merge(df1, df2, how='inner', on='Fruits')
Inner Join: Merge the two data frames df1 and df2 based on the common ‘Fruits’ name of both data frames.
pd.merge(df1, df2, how='outer', on='Fruits')
Outer Join:
Merge the two data frames df1 and df2 based on the common ‘Fruits’ name
In this case ‘Fruits’ of both data frames will be arranged accordingly
Note: This cheat sheet covers common commands and techniques in Pandas, a powerful library for data analysis and manipulation in Python. For detailed explanations and examples, please refer to the official Pandas documentation.