Home > Back-end >  How to create a column that returns the result of a function?
How to create a column that returns the result of a function?

Time:02-22

How to create a column that returns the result of a function?

Hello, on a dataframe that contains the bitcoin price, I want to measure the strength of buyers and sellers.

For the uninitiated in trading, I use a price representation called 'Japanese candle' which takes into account the opening, closing, highest and lowest price for each line : The shadows are the ends of the candlesticks located between the opening or closing and the top of the candle (for high shadow) and the bottom of the candle (for low shadow). example : https://en.wikipedia.org/wiki/Candlestick_chart

I want to add to the original dataframe, two new columns that will return for each row :

  • the size of the low shadow IF the price has increased (bullish candle)
  • the size of the high shadow IF the price has gone down (bearish candle)

My basic idea: 1.Create a new object/dataframe 'df_shadow', which contains a function to calculate the size of the shadow, depending on the direction of the prices. 2. Concatenate this 'df_shadow' with the original df that contains the price (Open, Close, High, Low) of the bitcoin to get the 2 new columns.

Unfortunately this doesn't work, certainly because I'm not very experienced in programming yet.

I thought of creating a function :

def low_shadow(x):
  for price in x:
    if x['Close'] > x['Open']:
      return(x['Open']-x['Low'])

then just a for loop :

for price in df['Close']:
  if df['Close']>df['Open']:
    print(df['Open']-df['Low']),
  else:
    print(df['Open']-df['Low'])

then just conditions :

df['low_shadow'] = [if df['Close'] > df['Open']:
                print(df['Open']-df['Low'])]

...but I didn't succeed. In the first 2 cases I get 'ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().' and in the last case 'SyntaxError: invalid syntax' from the for. How to create these 2 columns?

CodePudding user response:

If I fully understand your question, I think you can use lambda function.

df['low_shadow'] = df.apply(lambda x: x['Open']-x['Low'] if x['Close'] > x['Open'] else 'enter your value', axis=1)

Please, provide example of data and desired result if this answer doesn't help you.

CodePudding user response:

You are quite close. You can do this in a variety of ways in pandas, but one way would be:

 df['low_shadow'] = df.apply(lambda row: row['Open'] - row['Low']
                             if row['Close'] > row['Open'] 
                             else None, 
                             axis=1)

So what's happening here: df.apply (with second argument axis=1) takes a function and applies it to each row of a data frame, returning a Series that is the result of that function. This Series is then assigned to be a new column in your data frame.

  • Related