Skip to main content

Tired of vectorization, df.apply and df.map? How about an old school inplace-for-loop? :)

Usage - DataFrame

pip install 
    from a_pandas_ex_old_school_for_loop import pd_add_old_school_for_loop

    pd_add_old_school_for_loop()

    import pandas as pd

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



    df

    Out[3]:

         PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked

    0              1         0       3  ...   7.2500   NaN         S

    1              2         1       1  ...  71.2833   C85         C

    2              3         1       3  ...   7.9250   NaN         S

    3              4         1       1  ...  53.1000  C123         S

    4              5         0       3  ...   8.0500   NaN         S

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

    886          887         0       2  ...  13.0000   NaN         S

    887          888         1       1  ...  30.0000   B42         S

    888          889         0       3  ...  23.4500   NaN         S

    889          890         1       1  ...  30.0000  C148         C

    890          891         0       3  ...   7.7500   NaN         Q

    [891 rows x 12 columns]





    for item in df.ds_set_values_with_loop():

        try:

            if item['get_value']() > 2:

                item['set_value'](10000000)

        except Exception:

            continue





    df

    Out[5]:

         PassengerId  Survived    Pclass  ...        Fare Cabin  Embarked

    0              1         0  10000000  ...  10000000.0   NaN         S

    1              2         1         1  ...  10000000.0   C85         C

    2       10000000         1  10000000  ...  10000000.0   NaN         S

    3       10000000         1         1  ...  10000000.0  C123         S

    4       10000000         0  10000000  ...  10000000.0   NaN         S

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

    886     10000000         0         2  ...  10000000.0   NaN         S

    887     10000000         1         1  ...  10000000.0   B42         S

    888     10000000         0  10000000  ...  10000000.0   NaN         S

    889     10000000         1         1  ...  10000000.0  C148         C

    890     10000000         0  10000000  ...  10000000.0   NaN         Q

    [891 rows x 12 columns]

Usage - Series in a DataFrame

    from a_pandas_ex_old_school_for_loop import pd_add_old_school_for_loop

    pd_add_old_school_for_loop()

    import pandas as pd

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



    df

    Out[3]:

         PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked

    0              1         0       3  ...   7.2500   NaN         S

    1              2         1       1  ...  71.2833   C85         C

    2              3         1       3  ...   7.9250   NaN         S

    3              4         1       1  ...  53.1000  C123         S

    4              5         0       3  ...   8.0500   NaN         S

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

    886          887         0       2  ...  13.0000   NaN         S

    887          888         1       1  ...  30.0000   B42         S

    888          889         0       3  ...  23.4500   NaN         S

    889          890         1       1  ...  30.0000  C148         C

    890          891         0       3  ...   7.7500   NaN         Q

    [891 rows x 12 columns]





    for item in df.PassengerId.ds_set_values_with_loop():

        try:

            if item['get_value']() > 2:

                item['set_value'](10000000)

        except Exception:

            continue



    df

    Out[3]:

         PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked

    0              1         0       3  ...   7.2500   NaN         S

    1              2         1       1  ...  71.2833   C85         C

    2       10000000         1       3  ...   7.9250   NaN         S

    3       10000000         1       1  ...  53.1000  C123         S

    4       10000000         0       3  ...   8.0500   NaN         S

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

    886     10000000         0       2  ...  13.0000   NaN         S

    887     10000000         1       1  ...  30.0000   B42         S

    888     10000000         0       3  ...  23.4500   NaN         S

    889     10000000         1       1  ...  30.0000  C148         C

    890     10000000         0       3  ...   7.7500   NaN         Q

    [891 rows x 12 columns]

Update lists and dicts inside cells

from a_pandas_ex_old_school_for_loop import pd_add_old_school_for_loop

from random import randrange

pd_add_old_school_for_loop()

import pandas as pd

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

getrandomlist = lambda x: [

    [[randrange(1, 100), randrange(1, 100)] * 1] * randrange(1, 10)

]

df["listtest"] = df.PassengerId.map(getrandomlist)

df

Out[4]: 

     PassengerId  ...                                           listtest

0              1  ...         [[[88, 15], [88, 15], [88, 15], [88, 15]]]

1              2  ...  [[[15, 63], [15, 63], [15, 63], [15, 63], [15,...

2              3  ...             [[[46, 9], [46, 9], [46, 9], [46, 9]]]

3              4  ...         [[[71, 12], [71, 12], [71, 12], [71, 12]]]

4              5  ...  [[[55, 44], [55, 44], [55, 44], [55, 44], [55,...

..           ...  ...                                                ...

886          887  ...  [[[81, 15], [81, 15], [81, 15], [81, 15], [81,...

887          888  ...                                       [[[18, 70]]]

888          889  ...                                       [[[16, 35]]]

889          890  ...  [[[48, 54], [48, 54], [48, 54], [48, 54], [48,...

890          891  ...  [[[11, 99], [11, 99], [11, 99], [11, 99], [11,...   





for item in df.listtest.ds_set_values_with_loop():

    try:

        if item['get_value']() > 15:

            item['set_value'](15000000)

    except Exception:

        continue     





df

Out[6]: 

     PassengerId  ...                                           listtest

0              1  ...  [[[15000000, 15], [15000000, 15], [15000000, 1...

1              2  ...  [[[15, 15000000], [15, 15000000], [15, 1500000...

2              3  ...  [[[15000000, 9], [15000000, 9], [15000000, 9],...

3              4  ...  [[[15000000, 12], [15000000, 12], [15000000, 1...

4              5  ...  [[[15000000, 15000000], [15000000, 15000000], ...

..           ...  ...                                                ...

886          887  ...  [[[15000000, 15], [15000000, 15], [15000000, 1...

887          888  ...                           [[[15000000, 15000000]]]

888          889  ...                           [[[15000000, 15000000]]]

889          890  ...  [[[15000000, 15000000], [15000000, 15000000], ...

890          891  ...  [[[11, 15000000], [11, 15000000], [11, 1500000...

[891 rows x 13 columns]

Metadata

Release files for a-pandas-ex-old-school-for-loop 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for a-pandas-ex-old-school-for-loop 0.1
File Size Uploaded Provenance Actions
a_pandas_ex_old_school_for_loop-0.1.tar.gz 6.0 kB Without provenance Details for a_pandas_ex_old_school_for_loop-0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for a-pandas-ex-old-school-for-loop 0.1
File Interpreter ABI Platform Actions
a_pandas_ex_old_school_for_loop-0.1-py3-none-any.whl Python 3 none any Details for a_pandas_ex_old_school_for_loop-0.1-py3-none-any.whl

Total release size: 14.0 kB

Release files / a_pandas_ex_old_school_for_loop-0.1.tar.gz

Download URL a_pandas_ex_old_school_for_loop-0.1.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a23e373e16ed1c16128fb51e4b6308a69342726b65ff0f91fa9992bbcce740fe
BLAKE2b-256 checksum
How to use checksums
0014f1374dcb15db0fa7cf579c11d361365924ce2dbd97625837e9752758bbab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / a_pandas_ex_old_school_for_loop-0.1-py3-none-any.whl

Download URL a_pandas_ex_old_school_for_loop-0.1-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
503b91184050249c9ab92a201d4c0ae1e777e2f10e9ba7899ac4319b25d60c0b
BLAKE2b-256 checksum
How to use checksums
2915b73e01515dc3bc13459993ac1770aecf0f8a60f37f841818cd3eee534fd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for a-pandas-ex-old-school-for-loop 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page