python Flashcards
What line of code converts a Panda module Series to a Python list?
(ds.tolist())
What does the input code of a dictionary look like?
d1 = {‘a’: 100, ‘b’: 200, ‘c’:300, ‘d’:400, ‘e’:800}
How do you convert a pd.Series into a numpy array?
pd.Series(np_array)
What line of code changes the data type of given a column or a Series?
s2 = pd.to_numeric(s1, errors=’coerce’)
What line of code moves data in to a DataFrame?
df = pd.DataFrame(data=d)
What does the input code for data look like?
d = {‘col1’: [1, 2, 3, 4, 7, 11], ‘col2’: [4, 5, 6, 9, 5, 0], ‘col3’: [7, 5, 8, 12, 1,11]}
What line of code converts the first column of a DataFrame as a Series?
s1 = df.icol[:,0]
What line of code converts a given Series to an array?
a = s1.values
What does the input code for a series of lists look like?
s = pd.Series([
[‘Red’, ‘Green’, ‘White’],
[‘Red’, ‘Black’],
[‘Yellow’]])
What line of code converts Series of lists to one Series?
s = s.apply(pd.Series).stack().reset_index(drop=True)
What line of code sorts a given Series?
new_s = pd.Series(s).sort_values()
What line of code allows you to add some data to an existing Series?
new_s = pd.concat([s, pd.Series([500, “php”])], ignore_index=True)
What lines of code create a subset of a given series based on value and condition?
n = 6
new_s = s[s < n]
What lines of code change the order of index of a given series
s = pd.Series(data = [1,2,3,4,5], index = [‘A’, ‘B’, ‘C’,’D’,’E’])
…
s = s.reindex(index = [‘B’,’A’,’C’,’D’,’E’])
How would you create a mean and standard deviation for the data of a given Series?
print(s.mean())
print(s.std())
How do you print the first and last 5 columns of a dataset?
print(df.head(5))
print(df.tail(5))
Python Pandas printing most costly car name
df = df [[‘company’,’price’]][df.price==df[‘price’].max()]