User Tools

Site Tools


pandas_series

This is an old revision of the document!


dummy

append element to series

In [1]:
import pandas as pd
s = pd.Series(dtype='int')
N = 4
for i in range(N):
    s.at[i**2] = i
print(s)
0    0
1    1
4    2
9    3
dtype: int64

In [2]:
pd.__version__
Out[2]:
'1.2.1'

check if

check if a series is empty

Use pandas.Series.empty .

$ ipython

In [1]:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A':  []})
df1
Out[1]:
Empty DataFrame
Columns: [A]
Index: []

In [2]:
df1['A'].empty
Out[2]:
True

A series with just NaNs is considered “non-empty”. Drop the NaNs to make it “empty”.

In [3]:
df2 = pd.DataFrame({'A':  [np.nan]})
df2
Out[3]:
    A
0 NaN

In [4]:
df2['A'].empty
Out[4]:
False

In [5]:
df2['A'].dropna().empty
Out[5]:
True

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, 2, 3]).is_unique
Out[2]: 
True

In [3]: 
pd.Series([1, 2, 2]).is_unique
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, 2, 3, np.nan, np.nan]).is_unique
Out[4]: 
False

In [5]: 
pd.Series([1, 2, 3, np.nan, np.nan]).dropna().is_unique
Out[5]: 
True

For completeness

In [6]: 
pd.Series([1, 2, 2, np.nan, np.nan]).is_unique
Out[6]: 
False

In [7]: 
pd.Series([1, 2, 2, np.nan, np.nan]).dropna().is_unique
Out[7]: 
False

Ref:-

pandas_series.1707093867.txt.gz · Last modified: 2024/02/05 00:44 by raju