How To Select Only Values Of A DataFrame?


How To Select Only Values Of A DataFrame?

Table Of Contents:

  1. Syntax To Select Only Values Of A DataFrame.
  2. Examples Of Selecting Only Values.

(1) Syntax:

pandas.DataFrame.to_numpy()

OR

pandas.DataFrame.values

Description:

  • Only the values in the DataFrame will be returned, the axes labels will be removed.

Returns:

  • numpy.ndarray : The values of the DataFrame.

(2) Examples Of Returning Only Values :

Example-1

df = pd.DataFrame({'age':    [ 3,  29],
                   'height': [94, 170],
                   'weight': [31, 115]})
df.to_numpy()

Output:

array([[  3,  94,  31],
       [ 29, 170, 115]], dtype=int64)

Example-2

df2 = pd.DataFrame([('parrot',   24.0, 'second'),
                    ('lion',     80.5, 1),
                    ('monkey', np.nan, None)],
                  columns=('name', 'max_speed', 'rank'))
df2.to_numpy()

Output:

array([['parrot', 24.0, 'second'],
       ['lion', 80.5, 1],
       ['monkey', nan, None]], dtype=object)

Example-3

df3 = pd.DataFrame([('parrot',   24.0, 'second'),
                    ('lion',     80.5, 1),
                    ('monkey', np.nan, None)],
                  columns=('name', 'max_speed', 'rank'))
df3.values

Output:

array([['parrot', 24.0, 'second'],
       ['lion', 80.5, 1],
       ['monkey', nan, None]], dtype=object)

Leave a Reply

Your email address will not be published. Required fields are marked *