Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reminder of add_columns implementation #49

Closed
zbarry opened this issue Nov 13, 2018 · 4 comments
Closed

Reminder of add_columns implementation #49

zbarry opened this issue Nov 13, 2018 · 4 comments

Comments

@zbarry
Copy link
Collaborator

zbarry commented Nov 13, 2018

For myself, mostly:

Chaining implementation:

import pandas_flavor as pf

@pf.register_dataframe_method
def add_columns(df, **kwargs):
    # column already exists
    # v is not a scalar but is a different length from the dataframe
    
    for k, v in kwargs.items():
        df = df.add_column(k, v)
    return df

Example usage for copying repeating rows from a DataFrame into another:

df1.columns is of {'var1', 'var2', 'var3'}

column_order = ['var1', 'var2']

df2.add_columns(**{
    col: vals
    for col, vals in zip(column_order, df1[column_order].iloc[0]) 
})
@ericmjl
Copy link
Member

ericmjl commented Nov 13, 2018

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html

It appears add_columns can simply function as a syntactic sugar wrapper for assign. Fulfills the goal of a cleaner or more explicit API.

@zbarry
Copy link
Collaborator Author

zbarry commented Nov 13, 2018

We should confirm assign doesn't copy the underlying data.

@zbarry
Copy link
Collaborator Author

zbarry commented Nov 17, 2018

assign indeed copies the underlying data:

image

shrug.

I'll move forward with the implementation we came up with.

@zbarry
Copy link
Collaborator Author

zbarry commented Nov 20, 2018

🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants