pandas Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

import pandas library

A

import pandas as pd

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

create a hardcoded dataframe

A
dfHardCoded = pd.DataFrame(
    # records
    [
        ['Jan', 23, 62],
        ['Feb', 11, 50],
        ['Mar', 40, 45],
        ['Apr', 22, 26],
        ['May', 40, 60],
        ['Jun', 10, 62]
    ],
    # indices
    index = [ 0, 1, 2, 3, 4, 5],
    # column headers
    columns = [ "month", "lowest", "highest"]
  )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

read data.csv. It is a TSV

A

df = pd.read_csv(“data.csv”, sep=”\t”)

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

descriptive statistics table for dataframe “df”

A

df.describe()

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

first 5 elements of “df”

A

df.head()

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

last 10 elements of “df”

A

df.tail(10)

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

datatypes of “df”

A

df.dtypes

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

information about the indices of records of “df”

A

df.index

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

values of “df” (i.e. records) as a numpy array

A

df.values

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

sort values of “df” based on feature “color”, descending

A

df.sort(“color’”, ascending=False)

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

return “color” column of dataframe as a pandas series object

A

df[“color”]

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

return “color” and “price” columns of dataframe as a pandas dataframe object

A

df[“color”, “price”]

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

return a range of records between indices 13-50

A

df[13:51]

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

return “color” at indices 2 and 3

A

df.loc[2:4,[“color”]]

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

return price at indices 3 and 4. You don’t know the exact name of the price column, but you know it’s index is 7 in the array of columns

A

df.iloc[3:5,[7]]

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

return all records with “age” below 10

A

df[df.age < 10]

17
Q

return all records with “month” being “Jan” or “Feb”

A

df[df.month.isin([“Jan”, “Feb”])]

18
Q

assign 0 to “score” at records 50 to 100

A

df.loc[50:101, [“score”]] = 0

19
Q

export to “hello.csv”. Make it tab delimited

A

df.to_csv(“hello.csv”, sep=”\t”)