pandas_series
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
pandas_series [2021/09/15 19:57] – raju | pandas_series [2024/02/06 05:11] – raju | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== creating a series ===== | ||
+ | ==== create a series from a list ==== | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | 0 sun | ||
+ | 1 mon | ||
+ | 2 tue | ||
+ | dtype: object | ||
+ | </ | ||
+ | |||
+ | To assign an index | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | s sun | ||
+ | m mon | ||
+ | t tue | ||
+ | dtype: object | ||
+ | </ | ||
+ | |||
+ | To assign a name to the column | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | s sun | ||
+ | m mon | ||
+ | t tue | ||
+ | Name: day, dtype: object | ||
+ | </ | ||
+ | |||
+ | To assign a name to the index | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | letter | ||
+ | s sun | ||
+ | m mon | ||
+ | t tue | ||
+ | Name: day, dtype: object | ||
+ | </ | ||
+ | |||
+ | Column name is useful when converting the series to dataframe. | ||
+ | < | ||
+ | >>> | ||
+ | 0 | ||
+ | s sun | ||
+ | m mon | ||
+ | t tue | ||
+ | |||
+ | >>> | ||
+ | day | ||
+ | s sun | ||
+ | m mon | ||
+ | t tue | ||
+ | </ | ||
+ | |||
+ | If the series did not have a name to begin with but we desire to have one while converting to the dataframe | ||
+ | < | ||
+ | >>> | ||
+ | days | ||
+ | s sun | ||
+ | m mon | ||
+ | t tue | ||
+ | </ | ||
+ | |||
+ | The index name comes in handy while resetting the index | ||
+ | < | ||
+ | >>> | ||
+ | index day | ||
+ | 0 | ||
+ | 1 | ||
+ | 2 | ||
+ | >>> | ||
+ | letter | ||
+ | 0 s sun | ||
+ | 1 m mon | ||
+ | 2 t tue | ||
+ | </ | ||
+ | |||
===== dummy ===== | ===== dummy ===== | ||
==== append element to series ==== | ==== append element to series ==== | ||
Line 21: | Line 102: | ||
</ | </ | ||
+ | ==== return a random element ==== | ||
+ | Use pandas.Series.sample | ||
+ | |||
+ | Ref:- | ||
+ | * https:// | ||
===== check if ===== | ===== check if ===== | ||
==== check if a series is empty ==== | ==== check if a series is empty ==== | ||
Line 65: | Line 151: | ||
Used Python 3.9.4 and IPython 7.22.0 | Used Python 3.9.4 and IPython 7.22.0 | ||
+ | |||
+ | tags | check if a series has at least one element | ||
+ | |||
+ | ==== check if all elements in a series are unique ==== | ||
+ | Use pandas.Series.is_unique | ||
+ | |||
+ | < | ||
+ | In [1]: | ||
+ | import pandas as pd | ||
+ | |||
+ | In [2]: | ||
+ | pd.Series([1, | ||
+ | Out[2]: | ||
+ | True | ||
+ | |||
+ | In [3]: | ||
+ | pd.Series([1, | ||
+ | Out[3]: | ||
+ | False | ||
+ | </ | ||
+ | |||
+ | Missing values are treated as any other value. So if there are multiple NaNs, it will return True. If this is not desired, drop the NaNs first. | ||
+ | < | ||
+ | In [4]: | ||
+ | import numpy as np | ||
+ | pd.Series([1, | ||
+ | Out[4]: | ||
+ | False | ||
+ | |||
+ | In [5]: | ||
+ | pd.Series([1, | ||
+ | Out[5]: | ||
+ | True | ||
+ | </ | ||
+ | |||
+ | For completeness | ||
+ | < | ||
+ | In [6]: | ||
+ | pd.Series([1, | ||
+ | Out[6]: | ||
+ | False | ||
+ | |||
+ | In [7]: | ||
+ | pd.Series([1, | ||
+ | Out[7]: | ||
+ | False | ||
+ | </ | ||
+ | |||
+ | Using | pandas 1.5.3, python 3.11.4, ipython 8.12.0 | ||
+ | |||
+ | Ref:- | ||
+ | * https:// | ||
+ | * https:// | ||
+ |
pandas_series.txt · Last modified: 2024/02/06 05:18 by raju