1

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:

  • _buy/_sell - for the Column name
  • buy_ / sell_ - for the Column input

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")
3
  • Problem with my suggested function is col_input + ":_orders_date_" + hour would return as a string but it is actually a variable, how to fix this? Commented Mar 14, 2021 at 11:39
  • IIUC, you need to reference the global variables like sell_orders_date_1h by the string 'sell_orders_date_1h', in that case you could use globals dictionary for example you can do 'Date' + col : globals()[col_input + "_orders_date_" + hour] Commented Mar 14, 2021 at 11:47
  • Updated my answer to address calling a variable using a string. Commented Mar 14, 2021 at 11:51

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, not sure how colons made their way there, thanks for the answer.
@doomdaam hope this answers your question. If so, please mark it as correct using the little check mark :)
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.