Is it possible to copy a dataframe in the middle of a method chain to a new variable? Something like:
import pandas as pd
df = (pd.DataFrame([[2, 4, 6],
[8, 10, 12],
[14, 16, 18],
])
.assign(something_else=100)
.div(2)
.copy_to_new_variable(df_imag) # Imaginated method to copy df to df_imag.
.div(10)
)
print(df_imag) would then return:
0 1 2 something_else
0 1.0 2.0 3.0 50.0
1 4.0 5.0 6.0 50.0
2 7.0 8.0 9.0 50.0
.copy_to_new_variable(df_imag) could be replaced by df_imag = df.copy() but this would result in compromising the method chain.
Creating variables dynamically is not a good idea, but you can easily take advantage of mutable objects like dictionaries.
Adding a new DataFrame method to do this seamlessly:
Output:
If you don't want to monkey patch the DataFrame objects, use
pipe:printing
In the same line you can also add a chainable
printmethod, or again use a lambda inpipe:Output:
As a module
You could also create a module:
pandas_debug.pyThen in your code: