Python (PANDAS series)

Ace your homework & exams now with Quizwiz!

fruits = ["apple", "banana", "cherry"] x, y, z = fruits print(x) print(y) print(z)

Unpack a collection. If you have a collection of values in a list, tuple, etc. Python allows you to extract the values into variables. This is called unpacking.

print(p1.x)

Use the p1 object to print the value of x: class MyClass: x = 5 p1 = MyClass()

a = 4 A = "Sally" # A will not overwrite a

Variable names are case-sensitive. Upper case and lower case variables will create two different variables.

myVariableName = "John" (each word, except the first, starts with a capital letter)

Variable names with more than one word can be difficult to read. There are several techniques you can use to make them more readable. This is an example of Camel case.

x = 4 (a type of int) x = "Sally" (now a type of str)

Variables do not need to be declared with any particular type, and can even change type after they have been set.

import mymodule as mx

What is the correct syntax for creating an alias for a module?

from mymodule import person1

What is the correct syntax of importing only the person1 dictionary of the "mymodule" module?

print(dir(mymodule))

What is the correct syntax of printing all variables and function names for the "mymodule" module?

def __init__(self, name, age):

What is the correct syntax to assign a "init" function to a class? class Person: def [blank](self, name, age): self.name = name self.age = age

x.printname()

What is the correct syntax to execute the printname method of the object x?

The headers and 5 rows

When using the head() method, how many rows are returned if you do not specify the number?

x = "Python " y = "is " x = "awesome" print(x + y + z) (notice the space after "Python " and "is ", without them, the result would be "Pythonisawesome")

You can also use the + operator to output multiple variables

x = 5 y = "John" print(type(x)) print(type(y))

You can get the data type of a variable with the type() function

myvar, myVar, MYVAR, myvar2, my_var, _my_var

Legal variable names

x = "John" # is the same as x = 'John'

String variables can be declared either by using single quotes or double quotes.

x, y, z = "Orange", "Banana", "Cherry" print(x) print(y) print(z)

Many values to multiple variables. Python allows you to assign values to multiple variables in one line.

x = y = z = "Orange" print(x) print(y) print(z)

One value to multiple variables. You can assign the same value to multiple variables in one line.

x = "Python is awesome" print(x)

Output variables. The Python print() function is often used to output variables.

x = 5 y = "John" print(x, y)

The best way to output multiple variables in the print() function is to separate them with commas, which even support different data types

df.tail()

The head() method returns the first rows, what method returns the last rows?

my_variable_name = "John" (each word is separated by an underscore character)

This is an example of Snake case.

MyVariableName = "John" (each word starts with a capital letter)

This is an example of a Pascal case.

True

True or false: a correlation of -9.0 is considered to be a good correlation

True

True or false: a correlation of 0.9 is considered to be a good correlation

""" This is a comment written in more than one line """

Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it.

df.plot(kind = 'scatter', x = 'Duration', y = 'Calories')

Insert the correct syntax for specifying that the plot should be of type 'scatter'

df['Duration'].plot(kind = 'hist')

Insert the correct syntax for specifying that the plot should of type 'histogram'

pd.Series(mylist index = ["x", "y", "z"]

Insert the correct syntax to add the labels "x", "y", and "z" to a Pandas Series

myseries[0] (hint: the first value...programming languages count from 0 instead of from 1)

Insert the correct syntax to return the FIRST value of a Pandas Series called "myseries"

df.to_string()

Insert the correct syntax to return the entire DataFrame

Must start with a letter or the underscore character, can only contain alpha-numeric characters and underscores, are case sensitive, cannot be any of the Python keywords (cannot start with a number)

A variable can have a short name (like x or y) or a more descriptive name (age, carname, total_volume). Rules for Python variables. A variable name...

class MyClass: x = 5

Create a class named MyClass

x = lambda a : a

Create a lambda function that takes one parameter (a) and returns it

p1 = MyClass()

Create an object of MyClass called p1 class MyClass: x = 5

x = "awesome" def myfunc(): print("Python is " + x) myfunc()

Global variables. Variables that are created outside of a function (as in all of the examples above) are known as global variables. They can be used by everyone, both inside and outside of functions.

x = str(3) #will be '3' y = int(3) #y will be 3 z = float(3) #z will be 3.0

If you want to specify the data type of a variable, you can use casting.

2myvar, my-var, my var

Illegal variable names

x = "Python" y = "is" z = "awesome" print(x, y, z) (notice the space after "Python " and "is ", without them, the result would be "Pythonisawesome")

In the print function, you can output multiple variables, separated by a comma.

x = 5 y = "John" print (x+y)

In the print() function, when you try to combine a string and a number with the + operator, Python will give you an error

df.corr()

Insert a correct syntax for finding relationships between columns in a DataFrame

df.plot()

Insert a correct syntax for visualizing the data in DataFrame as a diagram (plotting)

pd.DataFrame(data)

Insert the correct Pandas method to create a DataFrame

pd.Series(mylist)

Insert the correct Pandas method to create a series

dropna(inplace = True)

Insert the correct argument to make sure that the changes are done for the original DataFrame instead of returning a new one

df.read_csv(data)

Insert the correct syntax for loading CSV files into a DataFrame

df.read_json(data)

Insert the correct syntax for loading JSON files into a DataFrame

pd.DataFrame(data)

Insert the correct syntax for loading a Python dictionary called 'data' into a DataFrame

df.drop_duplicates()

Insert the correct syntax for removing duplicates in a DataFrame

df.dropna()

Insert the correct syntax for removing rows with empty cells

df.fillna(130)

Insert the correct syntax for replacing empty cells with the value "130"

df.head(10)

Insert the correct syntax for returning the headers and the first 10 rows of a DataFrame

df.loc[0]

Insert the correct syntax to return the first row of a DataFrame


Related study sets

The Point: Chapter 27 Female Genitalia

View Set

Real Estate State Law Practice Exam 4, PRACTICE TEST 1, State Test Prep., Real Estate State Law Practice test 6, National Test, Exam 1, Nevada Real Estate, Real Estate

View Set

Vitamins & Minerals - Functions & Benefits

View Set

Purposive Communication and Four Model of communication

View Set

Abeka Kings of Israel 9th Grade Final Review

View Set

Assessment: Ch 25 - Assessing Neurologic System

View Set