Python Pandas 2 Flashcards

1
Q

Line to read a CSV file

A

import pandas as pd

df=pd.read_csv(“k.csv”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does df.head(3) do ?

A

Prints the first 3 entires

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does df.tail(3) do ?

A

Fetches the last 3 entries of all entries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the line to read excel sheets

A

excel_data_df = pandas.read_excel(‘records.xlsx’, sheet_name=’Employees’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you read a file that ISNT CSV but has different delimitter? 8:18

A

df = pd.read_csv(“pk_data.txt”.delimiter=’\t’ )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Line to get names of all columns in the data file

A

df.columns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Line to print one certain column

A

dt[‘Col_Name’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Line to get specific column of top 5 entries

A

dt[‘Col’][0:5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Line to get multple selective columns

A

dt[[‘Col1’,’Col2’]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Line to get all details concerning row at certain position

A

df.iloc[1]

(Getting entry at position 2 zero indexing applies)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Line to get all details concerning multiple first few row

A

df.iloc[0:2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Line to get a specific column OF a certain entry using ONLY INDICES

A

df.iloc[1,2]

1 is the second entry

And 2 stands for the 3rd column (We have to account for the zero indexing)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Line to iterate through all entries of datasheet

A

for index,df in df.iterrows():
print(index,df)

/////df stands for data frame/////

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Line to iterate through rows and print specific columns of those entries

A

for index,df in df.iterrows():
print(index,df[‘Name’])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Line to to fetch data entries that satisfy a certain condition(Search filters).

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Line to fetch data that has multiple filter layers

A

df.loc[df[‘Type 1’] == Grass].loc[df[‘Type 1’] == Fire]]

17
Q

Line to get standard deviation of a certain column

A

df[‘Age’].describe()[‘std’]

18
Q

What is the use of describe() is pandas

A

It displays data like count ,mean , min , std, 25percent, 75 and max

19
Q

Sort values in ascending order

A

df.sort_values(‘Power’)

20
Q

Sort names in leographic order

A

df.sort_values(‘Name’)

21
Q

Sort elements in descending order

A

df.sort_values(‘Name’,ascending = False)

22
Q

Sort first column with ascending and second column with desc

A

df.sort_values([‘Type1’,’HP’],ascending=[1,0])

23
Q

Line to Create new column which is function of previous columns

A

df[‘Total’] = df[‘HP’]+df[‘Attack’]-df[‘Damage’]

/////It automatically creates a new column for the rest ////

24
Q

Remove muliple columns

A

df = df.drop(columns = [‘Total1’,’Total2’])

25
Q

Getting stats for specific column

A

df[“Age”].describe()

26
Q

What does descibe function of pandas dataframe return me ?

A

It returns me a dictionary of various quantites

27
Q

Line to get a specific column (VIA LABEL) OF a certain entry (VIA INDEX)

A

first = data.iloc[0][‘Age’]

We used 0 as index for entry (Meaning first row ) and label as identifier for specific column (Age is the column)

28
Q

Line to get standard deviation of all columns

A

df.describe()[‘std’]

29
Q

Error debugging #1

What happens if you write print(df[Name])

A

It will raise an error because in df[Name] we should pass a stiring, not just Name(Which might work if it was defined as a string var)

30
Q

What is the difference between iloc and loc functions ?

A

loc is used for fetching entires based on boolean conditions

whereas

iloc is used for fetching results based on simple indices.

(Hence the use of i, which probably means index locator)

31
Q

Line for filtering data of a certain column using indexof column BUT NOT LABEL

A

df.loc[df.iloc[:,6]

32
Q

Line for printing specific entires based on comparision filters

A

df[df[‘Col1’]

33
Q

Line for printing specific columns AFTER performing a search criteria

A

df.loc[df[‘Units’]

34
Q

Line to replace all entries that contain specific element(string or value) with a DIFFERENT element

A

df.replace(to_replace =”Boston Celtics”,value =”Omega Warrior”)

35
Q

Line to replace all entries that contain specific element (string or value) that belong to a set with a DIFFERENT element

A

df.replace(to_replace =[“Boston Celtics”, “Texas”], value =”Omega Warrior”)