Pandas Commands

In this page i will list down the commands I learn to used with Pandas. Newer will be at top, the code is as a sample and not to cover all cases.

If you want to add some commands here just comment it below and i will add it.


:: Pandas Lessons Post ::

Lesson 1 Lesson 2 Lesson 3 Lesson 4
Lesson 5 Lesson 6 Lesson 7 Lesson 8
Lesson 9 Lesson 10 Lesson 11 Lesson 12
if ‘cage_no’ in df.columns:
hide_cage = df.drop([‘cage_no’], axis=1)
print(‘\n\n’,hide_cage.sample(6))
else:
print(‘Column not found’)
Output:L4: If a column in df action…
print(‘\n\n Drop column “supervisor” form the df’)
print(df.drop([‘supervisor’],axis=1))
Output:L4: Drop column from the df. To grop more columns add them to drop([‘c1′,’c2′,’cx’]
animal_cage_years=df[[‘animal’,’cage_no’,’years’]]
print(‘\n\n Show selected Columns from df\n’,animal_cage_years.sample(6))
Output:L4: To show only three columns of df.
hide_supervisor=df.drop([‘supervisor’], axis=1)
print(‘\n\n Sample data after hiding supervisor column\n’,hide_supervisors.sample(6))
Output:L4: Function to hide a column from df.
age_biger_7 = df[‘years’] > 7
df.where(mark_supervision & age_biger_7, inplace =True)
print(‘\n Only rows under mark supervision and animal age > 7\n’,df)
Output: Only showing data of animals under mark supervision and age >7.
mark_supervision = df[‘supervisor’] == ‘mark’
df.where(mark_supervision, inplace=True)
print(\n Only rows under mark supervision)
Output: df will show only rows under mark supervision other rows will be as NaN.
df.sort_values(‘supervisor’, inplace=True)
print(‘\n Sorted data by supervisor’)
Output: Sorted dataframe
all_exclude_lion_elephant = df.loc[(df.animal !=’lion’) & (df.animal !=’elephant’)]
print(‘\n\n Data without lion and elephant\n’)
Output:Printout: All dataframe without lion and elephants rows.
lion_elephant = dataf.loc[(dataf.animal==’lino’) | (dataf.animal==’elephant’)]
print(lion_elephant)

Output:PrintOut: all the rows for lino and elephants.
lion_rows=dataf.loc[(dataf.animal==’lion’)]

print(lion_rows)

Output:Print out all recodes with animal ‘lion’
age_less_6 = dataf.loc[(dataf.years < 6)]

print(age_less_6)

Output:Print out all the data that animal age is less than 6.
print(dataf.sample(4))

Output: print out 4 random rows of data, if we left the brackets emput, it will bring one random row.
print(dataf)

Output: print out all the data from the file
print(dataf.head())

Output: Print out first 5 rows of the data, if we add a number between (x), it will print first x rows.
print(dataf.tail())

Output: Print last 5 rows of the data, if we add a number between (x), it will print first x rows.
import pandas as pd

file_name=’YourFileName.csv’
dataf=pd.read_csv(YourFileName)

Output: We need this part to load the pandas Laibrary, and calling the CSV file, loading all the data in the file to ‘dataf’
Advertisements