Tip - Get row values from a specific row in a Dataframe

Screen Link:
https://app.dataquest.io/m/144/bar-plots-and-scatter-plots/4/creating-bars

I encountered this when studying Bar Plots. The Mission suggested this code for getting the bar_heights :
bar_heights = norm_reviews[num_cols].iloc[0].values

It seemed very complex and not so easy to memorize .
Looking at rows as ndarray is more intuitive to me than using the *.values * attribute so, I used an alternative method to do the same thing.
Thanks in advance for any input!

Method 1:

df.values    # gives ndarray  of the Dataframe
df.values[0]   # to get the first row or list

instead of the more complicated format of df. iloc[0].values

Method 2:

np.array(df)   # gives ndarray  of the Dataframe
np.array(df)[0]  # to get the first row or list

Since df. values is same as np.array(df), getting a row from the List of Lists would work similarly

df_array = norm_reviews[num_cols].values
print (type(df_array), df_array[0])

bar_array = np.array(norm_reviews[num_cols])
bar_heights = bar_array[0]
print (type(bar_array), bar_heights)

Output
<class ‘numpy.ndarray’> [4.3 3.55 3.9 4.5 5. ]
<class ‘numpy.ndarray’> [4.3 3.55 3.9 4.5 5. ]

So, to get a row of values from a DataFrame, I would

  1. Convert it to ndarray
  2. Get a row as a list as you would from a list of lists
2 Likes

@DnaData: Recategorized your topic.

2 Likes