doomdaam
doomdaam

Reputation: 783

Create pandas DataFrames in a function

How can I build a function that creates these dataframes?:

buy_orders_1h = pd.DataFrame(
{'Date_buy': buy_orders_date_1h,
 'Name_buy': buy_orders_name_1h
})

sell_orders_1h = pd.DataFrame(
{'Date_sell': sell_orders_date_1h,
 'Name_sell': sell_orders_name_1h
})

I have 10 dataframes like this I create very manually and everytime I want to add a new column I would have to do it in all of them which is time consuming. If I can build a function I would only have to do it once.

The differences between the two above function are of course one is for buy signals the other is for sell signals.

I guess the inputs to the function should be:

I'm thinking input to the function could be something like:

def create_dfs(col, col_input,hour):
    df = pd.DataFrame(
    {'Date' + col : col_input + "_orders_date_" + hour,
     'Name' + col : col_input + "_orders_name_" + hour
    }
    return df

buy_orders_1h = create_dfs("_buy", "buy_", "1h")
sell_orders_1h = create_dfs("_sell", "sell_", "1h")

Upvotes: 1

Views: 1807

Answers (2)

avi
avi

Reputation: 170

A dataframe needs an index, so either you can manually pass an index, or enter your row values in list form:

def create_dfs(col, col_input, hour):
    df = pd.DataFrame(
    {'Date' + col: [col_input + "_orders_date_" + hour],
     'Name' + col: [col_input + "_orders_name_" + hour]})
    return df

buy_orders_1h = create_dfs("_buy", "buy_", "1h")
sell_orders_1h = create_dfs("_sell", "sell_", "1h")

Edit: Updated due to new information: To call a global variable using a string, enter globals() before the string in the following manner:

'Date' + col: globals()[col_input + "_orders_date_" + hour]

Upvotes: 1

Karen
Karen

Reputation: 431

Check the output please to see if this is what you want. You first create two dictionaries, then depending on the buy=True condition, it either appends to the buying_df or to the selling_df. I created two sample lists of dates and column names, and iteratively appended to the desired dataframes. After creating the dicts, then pandas.DataFrame is created. You do not need to create it iteratively, rather once in the end when your dates and names have been collected into a dict.

from collections import defaultdict
import pandas as pd 
buying_df=defaultdict(list)
selling_df=defaultdict(list)


def add_column_to_df(date,name,buy=True):

    if buy:
        buying_df["Date_buy"].append(date)
        buying_df["Name_buy"].append(name)
    
    else:
        selling_df["Date_sell"].append(date)
        selling_df["Name_sell"].append(name)

dates=["1900","2000","2010"]
names=["Col_name1","Col_name2","Col_name3"]

for date, name in zip(dates,names):
    add_column_to_df(date,name)

#print(buying_df)

df=pd.DataFrame(buying_df)
print(df)

Upvotes: 0

Related Questions