基于此thread,我有一个后续问题。
ManelFornos Code正是我所需要的:
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))
但在我的数据框中,我有一个DateTime列,导致错误:
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)
抛出此错误:
ValueError: Shape of passed values is (3211, 2), indices imply (3211, 13)
有人知道如何解决这个问题吗?
谢谢! 即