0

based on this thread I have a follow-up question.

ManelFornos Code does exactly what I need:

import quandl
import MySQLdb
import pandas as pd

mysql_cn= MySQLdb.connect(host='localhost', port=3306,user='px_user', passwd='ProjectX2016', db='px')

df = quandl.get("WIKI/GOOGL")

columns = ["High", 'Low', 'Close']


def operations(row, columns):
    df1 = row[columns[0]] + row[columns[1]] + row[columns[2]]
    df2 = -row[columns[1]] - row[columns[2]] + row[columns[0]]
    return df1, df2

print(df.info())
df["function1"], df["function2"] = zip(*df.apply(lambda row: operations(row, columns), axis=1))

print(df.info())
print(df[["High","Low","Close","function1", "function2"]].head(5))

but in my dataframe, I have a DateTime Column, which results in an error:

import quandl
import MySQLdb
import pandas as pd

mysql_cn= MySQLdb.connect(host='localhost', port=3306,user='px_user', passwd='ProjectX2016', db='px')

df = quandl.get("WIKI/GOOGL")

columns = ["High", 'Low', 'Close']


df['DateTime'] = df.index

def operations(row, columns):
    df1 = row[columns[0]] + row[columns[1]] + row[columns[2]]
    df2 = -row[columns[1]] - row[columns[2]] + row[columns[0]]
    return df1, df2

print(df.info())
df["function1"], df["function2"] = zip(*df.apply(lambda row: operations(row, columns), axis=1))

print(df.info())
print(df[["High","Low","Close","function1", "function2"]].head(5)

this error is thrown:

ValueError: Shape of passed values is (3211, 2), indices imply (3211, 13)

Does anybody know how to solve this?

Thanks! e.

2
  • 1
    Wow, that's a weird one... what happens when you do df["function1"], df["function2"] = zip(*df.drop('DateTime', axis=1).apply(lambda row: operations(row, columns), axis=1))? Commented May 20, 2017 at 13:48
  • @BobHaffner yeah. that solves my issue!!! thanks! Commented May 20, 2017 at 14:31

0

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.