Python Flashcards

1
Q

Attribute that returns data type for a series or dataframe

A

Series.dtype

Dataframe.dtype

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

pandas method to show first n (5 is default) rows in a series

A

Series.head()

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

pandas method to show last n (5 is default) rows in a series

A

Series.tail()

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

pandas method to return unique values in a series

A

Series.unique()

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

pandas method to view the highest and lowest values in a series with their counts

A

Series.sort_index()with ascending=True orFalse

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

pandas method to count unique values in a series

A

Series.value_counts()
dropna=False includes null
normalize=True to do %

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

pandas method to remove/replace specified text

A

Series.str.replace([‘text_to_replace’],’’)

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

pandas method to cast a pandas object to a specified type (ex: cast string to float or int)

A

Series.astype(float)

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

pandas method chaining to replace text and cast to number

A

Series.str.replace([‘text_to_replace’],’’).astype(float)

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

pandas method to detect missing values and return a boolean same-sized object indicating if the values are NA.

A

Series.isnull()

Ex: Select null values in column
rev_is_null = f500[“revenue”].isnull()

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

pandas method to detect existing (non-missing) values and return a boolean same-sized object indicating if the values are not NA.

A

Series.notnull()

Ex: Select non-null values in a column
rev_not_null = f500[f500[“revenue”].notnull()

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

pandas method to generate descriptive statistics such as central tendency, dispersion and shape of a dataset’s distribution, excluding NaN values. Analyzes both numeric and object series, as well as DataFrame column sets of mixed data types. The output will vary depending on what is provided.

A

Series.describe()

include = ‘all’ to include non-numeric columns

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

pandas method to rename series index labels or name.

A

Series.rename({“ram”:”ram_gb”}, axis = ‘columns’, inplace = True)

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

pandas method to remove whitespace from start and end of string

A

String.strip()

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

pandas method to convert string to lowercase

A

String.lower()

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

pandas attribute that returns column names for dataframe

A

DataFrame.columns

17
Q

pandas method to print a concise summary of a DataFrame, including including the index dtype and column dtypes, non-null values and memory usage.

A

DataFrame.info()

18
Q

pandas method to remove or drop rows and columns with null values

A

DataFrame.dropna()
axis = 0 drops rows
axis = 1 drops columns

19
Q

pandas method to map values of Series according to input correspondence.

Used for substituting each value in a Series with another value, that may be derived from a function, a dict or a Series.

A

Series.map(mapping_dictionary)

Ex:
s = pd.Series([‘fox’, ‘cow’, np.nan, ‘dog’])
s.map({‘fox’: ‘cub’, ‘cow’: ‘calf’})

If a value from your series doesn’t exist as a key in your dictionary, it will convert that value to NaN

20
Q

pandas function to read in a csv file

A

f = pd.read_csv(‘[file_name]’, encoding=’[encoding_type]’)

encoding type examples = Latin-1, UTF-8, Windows-1251

21
Q

pandas method to export cleaned data (pandas)

A

DataFrame.to_csv(“file_name”, index=False)

22
Q

Attribute to access labels of a series

A

Series.index

Ex: Series.value_counts().head(10).index

23
Q

Sort a dictionary in ascending order based on values (not keys)

A

sort_d = sorted(dictionary.items(), key=lambda kv:(kv[1], kv[0])))

24
Q

pandas method to remove or drop rows or columns from a dataframe based on specified values

A

DataFrame.drop([“col_name1”, “col_name2”], axis=1)

 Change axis to 0 to drop rows
25
Q

Select portion of data between 2 values

A

DataFrame[DataFrame[“column”].between(min, max)]