Created
December 28, 2018 18:37
-
-
Save cynddl/3c83e8f15dd519bef0b4f5b609b8a9b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_dataframe(columns=None, iter=None): | |
""" | |
Decorator to return a pandas DataFrame from an iterator. | |
""" | |
def decorator(f): | |
@wraps(f) | |
def wrapper(*args, **kwds): | |
arr = list(f(*args, **kwds)) | |
iter_columns = [] | |
if columns is not None and iter is not None: | |
range_iter = range(1, len(arr[0]) - len(columns) + 1) | |
iter_columns = ['{}_{}'.format(iter, i) for i in range_iter] | |
return pd.DataFrame(arr, columns=columns + iter_columns) | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment