Python Pandas 2 Flashcards
Line to read a CSV file
import pandas as pd
df=pd.read_csv(“k.csv”)
What does df.head(3) do ?
Prints the first 3 entires
What does df.tail(3) do ?
Fetches the last 3 entries of all entries
What is the line to read excel sheets
excel_data_df = pandas.read_excel(‘records.xlsx’, sheet_name=’Employees’)
How do you read a file that ISNT CSV but has different delimitter? 8:18
df = pd.read_csv(“pk_data.txt”.delimiter=’\t’ )
Line to get names of all columns in the data file
df.columns
Line to print one certain column
dt[‘Col_Name’]
Line to get specific column of top 5 entries
dt[‘Col’][0:5]
Line to get multple selective columns
dt[[‘Col1’,’Col2’]]
Line to get all details concerning row at certain position
df.iloc[1]
(Getting entry at position 2 zero indexing applies)
Line to get all details concerning multiple first few row
df.iloc[0:2]
Line to get a specific column OF a certain entry using ONLY INDICES
df.iloc[1,2]
1 is the second entry
And 2 stands for the 3rd column (We have to account for the zero indexing)
Line to iterate through all entries of datasheet
for index,df in df.iterrows():
print(index,df)
/////df stands for data frame/////
Line to iterate through rows and print specific columns of those entries
for index,df in df.iterrows():
print(index,df[‘Name’])
Line to to fetch data entries that satisfy a certain condition(Search filters).
df. loc[df[‘Type 1’] == Grass]
df. loc[‘Type 1’] returns all data strings under Type 1 column and then the condition checks which of them are ‘Grass’