Write a function named last_n_if_long that takes a DataFrame df and a number n, and returns the last n rows if df has more rows than n, or df if not.
>>> last_n_if_long(pd.DataFrame({"n": [8, 9, 1, 0]}), 3)
n
1 9
2 1
3 0
Create a Series of 2 strings with index ['Fier', 'Vlora'], assign it to s, and print s.
Write a function named is_wide that takes a Series of temperatures s and numbers low and high, and returns the mask of temperatures greater than high or less than low.
>>> s = pd.Series([9, 20, 4, 30]) >>> is_wide(s, 5, 25) 0 False 1 False 2 True 3 True dtype: bool
Write a function named wide_total that takes a Series of temperatures s and numbers low and high, and returns the total of those temperatures. wide_total must use is_wide.
>>> s = pd.Series([2, 18, 40, 6]) >>> wide_total(s, 5, 20) 42
Create a Series from [58, 47, 61, 40, 33] and assign it to s.
Keep the values of s that are greater than 50, assign them to kept, and print kept.
Write a function named is_long that takes a DataFrame df and a number n and returns True if df has more rows than n, or False if not.
>>> is_long(pd.DataFrame({"n": [4, 5, 6, 7]}), 2)
True
Write a function named first_if_long that takes a DataFrame df and a number n and returns the first n rows if df has more rows than n, or df if not. first_if_long must use is_long.
>>> first_if_long(pd.DataFrame({"n": [1, 8, 3, 9]}), 2)
n
0 1
1 8
Create a Series of 3 numbers with index ['Peja', 'Oslo', 'Recife'] and assign it to s.
Get the value of s at Oslo using the label, assign it to got, and print got.
Create a Series and assign it to s.
Get the first value of s using position, assign it to x, and print x.
Write a function named kinds_over that takes a Series s and a number n, and returns how many distinct values of s are greater than n.
>>> s = pd.Series([4, 11, 4, 18, 11, 3]) >>> kinds_over(s, 10) 2
Call kinds_over with pd.Series([7, 20, 20, 1, 14]) and 10, assign the result to n, and print n.
Write a function named with_int_col that takes a DataFrame df and a column name col, and returns a copy of df with that column as whole numbers.
>>> df = pd.DataFrame({"bin": ["7", "2"], "ware": ["mug", "pot"]})
>>> with_int_col(df, "bin")
bin ware
0 7 mug
1 2 pot