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.

Community
  • 1
  • 1
Ele
  • 523
  • 2
  • 6
  • 19
  • 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))`? – Bob Haffner May 20 '17 at 13:48
  • @BobHaffner yeah. that solves my issue!!! thanks! – Ele May 20 '17 at 14:31

0 Answers0