Best practice for cleaning Pandas dataframe columns I am cleaning str columns in a Pandas dataframe (see below for an example), and I have been wondering if there are more concise ways or additional inplace methods to do so. What are the general best practices for cleaning columns in Pandas?
import pandas as pd
df = pd.DataFrame.from_dict({&quot;col1&quot;: [0, 1, 2, 3], &quot;col2&quot;: [&quot;abcd efg&quot;, &quot;.%ues&quot;, &quot;t12 ^&amp;3&quot;, &quot;yupe&quot;]})

df[&quot;col2&quot;] = df[&quot;col2&quot;].str.lower()
df[&quot;col2&quot;] = df[&quot;col2&quot;].str.strip()
df[&quot;col2&quot;].replace(to_replace=&quot;[^a-zA-Z ]&quot;, value=&quot;&quot;, regex=True, inplace=True)
This is not too bad. It's a good thing you use keyword arguments for the replace method
I always try to keep my original data in its original state, and continue with the cleaned dataframe.
fluent style
This lends itself very well to a kind of fluent style as in this example. I use it too, and use a lot of df.assign, df.pipe, df.query...
In this example I would do something like
df_cleaned = df.assign(
    col2=(
        df[&quot;col2&quot;]
        .str.lower()
        .str.strip()
        .replace(to_replace=&quot;[^a-zA-Z ]&quot;, value=&quot;&quot;, regex=True)
    )
)

So definately no inplace replacements