Skip to main content

One more try if df.drop_duplicates() throws an exception

Project description

Installation

a-pandas-ex-drop-duplicates-without-pain 

Usage

from a_pandas_ex_drop_duplicates_without_pain import pd_add_drop_duplicates_without_pain

pd_add_drop_duplicates_without_pain()

import pandas as pd

df = pd.read_csv("https://github.com/pandas-dev/pandas/raw/main/doc/data/air_quality_long.csv")

print(f'{df=}')

df_with_duplicates = df[['city', 'country',  'location', 'parameter', 'value','unit']].copy()

print(f'{df_with_duplicates=}')

df_without_duplicates = df_with_duplicates[['city', 'country',  'location', 'parameter', 'value',      'unit']].drop_duplicates().copy()

print(f'{df_without_duplicates=}')

df_with_duplicates['provoke_error'] = [[[1]*10]] * len(df_with_duplicates)

print(f'{df_with_duplicates=}')

df_result1 = None

df_result2 = None

try:

    df_result1=df_with_duplicates.drop_duplicates()

except Exception as Err:

    print(Err)

    df_result2=df_with_duplicates.ds_drop_duplicates_without_pain()

print(f'{df_result1=}')

print(f'{df_result2=}')

df.parameter.ds_drop_duplicates_without_pain()

df=           city country                   date.utc  ... parameter value   unit

0     Antwerpen      BE  2019-06-18 06:00:00+00:00  ...      pm25  18.0  µg/m³

1     Antwerpen      BE  2019-06-17 08:00:00+00:00  ...      pm25   6.5  µg/m³

2     Antwerpen      BE  2019-06-17 07:00:00+00:00  ...      pm25  18.5  µg/m³

3     Antwerpen      BE  2019-06-17 06:00:00+00:00  ...      pm25  16.0  µg/m³

4     Antwerpen      BE  2019-06-17 05:00:00+00:00  ...      pm25   7.5  µg/m³

         ...     ...                        ...  ...       ...   ...    ...

5267     London      GB  2019-04-09 06:00:00+00:00  ...       no2  41.0  µg/m³

5268     London      GB  2019-04-09 05:00:00+00:00  ...       no2  41.0  µg/m³

5269     London      GB  2019-04-09 04:00:00+00:00  ...       no2  41.0  µg/m³

5270     London      GB  2019-04-09 03:00:00+00:00  ...       no2  67.0  µg/m³

5271     London      GB  2019-04-09 02:00:00+00:00  ...       no2  67.0  µg/m³

[5272 rows x 7 columns]

df_with_duplicates=           city country            location parameter  value   unit

0     Antwerpen      BE             BETR801      pm25   18.0  µg/m³

1     Antwerpen      BE             BETR801      pm25    6.5  µg/m³

2     Antwerpen      BE             BETR801      pm25   18.5  µg/m³

3     Antwerpen      BE             BETR801      pm25   16.0  µg/m³

4     Antwerpen      BE             BETR801      pm25    7.5  µg/m³

         ...     ...                 ...       ...    ...    ...

5267     London      GB  London Westminster       no2   41.0  µg/m³

5268     London      GB  London Westminster       no2   41.0  µg/m³

5269     London      GB  London Westminster       no2   41.0  µg/m³

5270     London      GB  London Westminster       no2   67.0  µg/m³

5271     London      GB  London Westminster       no2   67.0  µg/m³

[5272 rows x 6 columns]

df_without_duplicates=           city country            location parameter  value   unit

0     Antwerpen      BE             BETR801      pm25   18.0  µg/m³

1     Antwerpen      BE             BETR801      pm25    6.5  µg/m³

2     Antwerpen      BE             BETR801      pm25   18.5  µg/m³

3     Antwerpen      BE             BETR801      pm25   16.0  µg/m³

4     Antwerpen      BE             BETR801      pm25    7.5  µg/m³

         ...     ...                 ...       ...    ...    ...

5087     London      GB  London Westminster       no2   81.0  µg/m³

5090     London      GB  London Westminster       no2   83.0  µg/m³

5091     London      GB  London Westminster       no2   76.0  µg/m³

5092     London      GB  London Westminster       no2   70.0  µg/m³

5098     London      GB  London Westminster       no2   79.0  µg/m³

[819 rows x 6 columns]

df_with_duplicates=           city country  ...   unit                     provoke_error

0     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

1     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

2     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

3     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

4     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

         ...     ...  ...    ...                               ...

5267     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

5268     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

5269     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

5270     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

5271     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

[5272 rows x 7 columns]

unhashable type: 'list'

df_result1=None

df_result2=           city country  ...   unit                     provoke_error

0     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

1     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

2     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

3     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

4     Antwerpen      BE  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

         ...     ...  ...    ...                               ...

5087     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

5090     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

5091     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

5092     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

5098     London      GB  ...  µg/m³  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

[819 rows x 7 columns]

Out[2]: 

0       pm25

1825     no2

Name: parameter, dtype: object

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

File details

Details for the file a_pandas_ex_drop_duplicates_without_pain-0.1.tar.gz.

File metadata

File hashes

Hashes for a_pandas_ex_drop_duplicates_without_pain-0.1.tar.gz
Algorithm Hash digest
SHA256 c229346560d93c20df6ba1e9c046f1062042c1eaf9a0e9e7b03e3bd408c34295
MD5 7a0cf4b1f90b59ecc409b9be6cbf7293
BLAKE2b-256 b031d7b8d0082318e149db5599f5bafcfce4b5e14caa0a0548f552c6940d707a

See more details on using hashes here.

Provenance

File details

Details for the file a_pandas_ex_drop_duplicates_without_pain-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for a_pandas_ex_drop_duplicates_without_pain-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fd3dcbcf38346d713bf74a3f3ad9cbb03fea7e6f4c95c7f65703dc94e1fdf298
MD5 d63b03514122b3353d5ffed2436187dc
BLAKE2b-256 7a61fe667a420c9ef55dfb08a3e459118a2cb927f1a85ea09baadc1d4a8533e7

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page