w3resource

Pandas Practice Set-1: Access a specified Series index and the Series values of diamonds DataFrame


Write a Pandas program to access a specified Series index and the Series values of diamonds DataFrame.

Sample Solution:

Python Code:

import pandas as pd
diamonds = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/diamonds.csv')
print("Original Dataframe:")
print(diamonds.head())
print("\nAccess the Series index:")
print(diamonds.carat.value_counts().index)
print("\nAccess the Series values:")
print(diamonds.carat.value_counts().values)

Sample Output:

Original Dataframe:
   carat      cut color clarity  depth  table  price     x     y     z
0   0.23    Ideal     E     SI2   61.5   55.0    326  3.95  3.98  2.43
1   0.21  Premium     E     SI1   59.8   61.0    326  3.89  3.84  2.31
2   0.23     Good     E     VS1   56.9   65.0    327  4.05  4.07  2.31
3   0.29  Premium     I     VS2   62.4   58.0    334  4.20  4.23  2.63
4   0.31     Good     J     SI2   63.3   58.0    335  4.34  4.35  2.75

Access the Series index:
Float64Index([ 0.3, 0.31, 1.01,  0.7, 0.32,  1.0,  0.9, 0.41,  0.4, 0.71,
              ...
              2.65, 3.05, 2.71, 3.22, 4.13,  2.7, 3.67, 5.01, 2.77,  3.4],
             dtype='float64', length=273)

Access the Series values:
[2604 2249 2242 1981 1840 1558 1485 1382 1299 1294 1258 1189 1127  910
  883  817  807  793  764  709  706  670  667  645  625  572  570  523
  496  492  492  488  475  473  440  430  398  394  381  373  361  342
  322  310  308  300  293  287  284  282  279  278  265  254  253  251
  251  251  249  246  246  236  233  228  226  220  215  212  212  207
  204  200  198  187  187  178  177  174  172  155  149  146  142  140
  135  134  133  131  130  126  124  123  122  122  119  110  110  109
  106  106  103  102  101   99   95   89   89   89   87   86   80   77
   68   67   65   65   64   64   63   62   61   60   59   59   57   52
   52   50   50   50   50   50   48   48   48   46   45   45   43   43
   41   40   40   36   34   32   32   31   31   31   30   28   27   26
   26   25   25   25   25   25   24   23   23   23   22   22   21   21
   21   21   21   20   19   19   18   18   18   18   18   17   17   17
   17   16   16   15   15   15   14   13   13   13   13   12   12   12
   12   11    9    9    9    9    9    9    9    8    8    8    8    8
    7    7    7    7    7    6    6    6    5    5    5    5    4    4
    4    4    4    4    4    3    3    3    3    3    3    3    3    3
    3    3    3    3    3    3    3    3    2    2    2    2    2    2
    1    1    1    1    1    1    1    1    1    1    1    1    1    1
    1    1    1    1    1    1    1]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a Pandas program to set an existing column as the index of diamonds DataFrame and restore the index name, and move the index back to a column.
Next: Write a Pandas program to sort a Series by its values and index of diamonds DataFrame.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.