Python (Freecodecamp)

Pandas, Numpy, Jupyter

133 flashcards · Programming Languages · by charlie42

  1. Convert to a decimal number

    float(10)

  2. Convert to integer

    int(10.1)

  3. int(9.9) vs round(9.9)

    9 10

  4. Convert to string

    str(10)

  5. Can list contain values of different data types?

    Yes! ['Facebook', 0.0, 'USD', 2974676, 3.5]

  6. Get size of a list

    len(list)

  7. Get last element of a list

    list[-1]

  8. What's list slicing

  9. Select first 3 elements of a list

    list[:3]

  10. Select last 3 elements of a list

    list[-3:]

  11. list[2:4] selects which elements?

    2, 3

  12. open file

    open(filepath)

  13. how to import a method of a module

    import method from module

  14. sum of all elements in a list

    sum(list)

  15. syntax for else if

    elif condition == True:

  16. what does python try to do when creating a new key of dictionary?

    convert it to an integer hash(key)

  17. two things you can't do with dictionaries

    use list or a dictionary as a key (can't be hashed into an integer) use both boolean value and integers 0 and 1 as keys, they will override each other since hash(True) == 1

  18. content_ratings = {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622} print(4433 in content_ratings)

    False Because in used on a dictionary checks in keys

  19. for loop

    for item in list:

  20. if value between two values

    if 10000 < user_ratings <= 100000:

  21. initialize a function

    def do_a_thing(input): return output

  22. import numpy

    import numpy as np

  23. create numpy array

    my_array = np.array([1, 2, 3])

  24. What does SIMD stand for?

    Single Instruction Multiple Data

  25. Concept of replacing for loops with operations applied to multiple data points at once

    Vectorization

  26. tuple vs list

    you can't modify a tuple

  27. get dimensions of a numpy array

    my_array.shape

  28. select single item from a list of list, and from 2d numpy array

    list[1][2] array[1,2]

  29. select first row of numpy array

    array[0]

  30. select specific columns of a numpy array

    array[:, [1,5,10]]

  31. select rows 100 to 200 of a numpy array

    array[100:201]

  32. np function vs method examples

    function: np.median(array) method: array.max()

  33. sum of each row of a 2d numpy array

    array.sum(axis=1)

  34. Jupyter notebook: create cell above

    A

  35. Jupyter notebook: create cell below

    B

  36. Jupyter notebook: delete cell

    DD

  37. Jupyter notebook: enter edit mode

    Enter

  38. Jupyter notebook: enter command mode

    Esc

  39. Jupyter notebook: switch to markdown

    M

  40. Jupyter notebook: switch to code mode

    Y

  41. Jupyter notebook: run and switch cell

    shift+Enter

  42. Jupyter notebook: run and keep current cell

    command+Enter

  43. Jupyter notebook: cut/pase

    X/V

  44. 2 to the power of 3

    2 ** 3

  45. Numpy: fill row 2 of array with zeros

    array[2] = 0 (will expand to the whole row)

  46. a[::3]

    every third element in the sequence seq[start:end:step]

  47. Numpy: select first and last value of a = [1, 2, 3, 4] with binary indices

    a[[True, False, False, True]]

  48. Numpy: select all values of a=[1,2,3,4] that are NOT >2

    a[ ~(a>2) ]

  49. Numpy: syntax for and and or

    &, |

  50. Numpy: Create a numpy array of size 10, filled with zeros

    np.zeros(10)

  51. Numpy: Create a numpy array with values ranging from 10 to 49

    np.arange(10, 50)

  52. Numpy: Create a numpy matrix of 2*2 integers, filled with ones

    np.ones([2,2], dtype=np.int)

  53. Numpy: Create a numpy array with the odd numbers between 1 to 10

    np.arange(1, 11, 2)

  54. Numpy: Given the X numpy array, show the elements in reverse position

    X[::-1]

  55. Pandas: 3 differences between Pandas Series and a Python List

    Series has a name Series has a type You can change indices of a series arbitrarily (can be strings)

  56. Pandas: 1 difference and 1 similarity between Pandas Series and a Python Dictionary

    Difference: Series is ordered Similarity: Series keys (indices) and values

  57. Pandas: Two ways to index a Series (code)

    my_series['my_index'] my_series.iloc[i]

  58. Pandas: difference in accessing by indices between Pandas Series and Python List

    Last index not included in python lists, but included in Series

  59. Pandas: read csv

    pd.read_csv(file_path)

  60. Pandas: get statistics on dataset

    ds.describe()

  61. Pandas: What is a dataframe?

    A collection of Series

  62. Pandas: command to see how many non-null values there are in every column in the dataframe?

    df.info()

  63. Pandas: get types of each column in a dataframe?

    df.dtypes()

  64. Pandas: how to index a row of a dataframe (2 ways)? What type is returned?

    df.loc['France'] df.iloc[2] Series

  65. Pandas: how to index a column of a dataframe? What type is returned?

    df['Population'] Series

  66. Pandas: select the "Population" column for all rows from "France" to "Italy" from a dataframe

    df.loc['France':'Italy', 'Population'] (upper limit is included)

  67. Pandas: select the columns 1 to 5 for rows 2 and 10 from a dataframe

    df.iloc[1:6, [2, 10]] (upper limit is not included)

  68. Pandas: get columns "Population" and "GDP" for countries with population > 50'000 from a dataframe

    df.loc[ df['Population'] > 50000 ], ['Population', 'GDP'] ]

  69. Pandas: why do we use loc to filter dataframe by condition?

    Because we filter rows by the index. The boolean array is indexed

  70. Pandas: drop row with index "Canada" from a dataframe

    df.drop('Canada')

  71. Pandas: drop rows with indices "Canada" and "France" from a dataframe

    df.drop(['Canada', 'France'])

  72. Pandas: drop column "Population" from a dataframe

    df.drop(columns=['Population'])

  73. Pandas: example of broadcasting

    Create a Series with indices matching the columns of the dataframe Subtract the Series from the dataset (The values from the Series will be substracted from the corresponding column of each row of the dataframe)

  74. Pandas: does the drop operation modify the original dataframe?

    No

  75. Pandas: does this operation modify the original dataframe: df['Language'] = "English" ?

    Yes

  76. Pandas: how do we know if an operation will modify the original dataframe?

    Usually the operations with an equals sign (=) in them will modify the dataframe, others won't

  77. Get signature of a method

    pd.read_csv?

  78. Pandas: read csv without headers, add headers manually

    df = read_csv('filepath.csv', header=None) df.columns = ['First column', 'Second column']

  79. Pandas: two ways to create a Series

    From a dictionary: s = pd.Series({ 'key1': 'value1', 'key2': 'value2' }, name = 'Column name') From an list: s = pd.Series( ['value1', 'value2'], index = ['key1', 'key2'], name = 'Column name' )

  80. Pandas: filter Series by condition

    s[s > 100]

  81. Pandas: not, and, and not symbols when filtering Series

    & | ~

  82. Pandas: create a Series of a type float

    pd.Series([1,2], dtype=float)

  83. Pandas: sort a Series

    X.sort_values()

  84. Pandas: show just the "sex" column of the first 5 elements on marvel_df

    marvel_df.iloc[:5]["sex"].to_frame() .loc will only work if marvel_df uses default pandas indexing (0, 1, 2, ...), which is not confirmed.

  85. Pandas: modify the first_appearance of 'Vision' to year 1964

    marvel_df.loc['Vision', "first_appearance"] = 1964

  86. Pandas: plot the values of a column

    df.column.plot()

  87. Pandas: plot the histogram of a column

    df.column.plot.hist()

  88. Pandas: read a df from csv without headers, assign headers manually, set the index, and parse dates in one command

    df = pd.read_csv( 'data/btc-market-price.csv', header=None, names=['Timestamp', 'Price'], index_col=0, parse_dates=True )

  89. Pandas: 3 methods specific for column types. Example

    str dt cat df['name'].str.split('-')

  90. 3 + np.nan

    np.nan Any arithmetic operation involving np.nan returns np.nan, as NaN is treated as an undefined or missing value.

  91. 3 + None

    TypeError None cannot be used in arithmetic operations with numbers in Python, so this expression raises a TypeError.

  92. Filter nan values from a numpy array

    a[~np.isnan(a)]

  93. np.isfinite(np.nan)

    False

  94. pd.isnull(np.nan)

    True

  95. pd.isnull(None)

    True

  96. pd.isna(np.nan)

    True

  97. pd.isna(None)

    True

  98. Opposite of pd.isna and pd.isnull

    pd.notna() pd.notnull()

  99. np.array([1, np.nan, 2]).sum()

    np.nan

  100. pd.Series([1, np.nan, 2]).sum()

    3