全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 python论坛
2818 2
2019-05-28
这本书的4.3.4节数据转化,设置哑变量
不懂是哪里代码敲错了
import pandas as pd
df_industry=DataAPI.EquIndustryGet(secID=u"",ticker=u"000001,600001",industryVersionCD=u"010303",industry=u"",industryID=u"",industryID1=u"",industryID2=u"",industryID3=u"",intoDate=u"20180101",field=u"secID,industryName1",pandas="1")
industry_list=df_industry['industryName1'].drop_duplicates().tolist()

def get(x):
    ind_s=pd.Series([0]*len(industry_list),index=industry_list)# Series是一个一维的数据结构,用list, index构建Series
    if len(df_industry[df_industry['secID']==x])>0:
        ind=df_industry[df_industry['secID']==x]['industryName1'].value[0]
        ind_s.loc[ind]=1
    #print ind_s
    return ind_s

df_[industry_list]=df_['secID'].apply(lambda x: get(x))
df_head()


提示NameError                                 Traceback (most recent call last)
<mercury-input-8-BE404B509ABF440F82C31EAC8FA23539> in <module>()     11     return ind_s     12 ---> 13 df_[industry_list=df_['secID'.apply(lambda x: get(x))     14 df_head()     15 NameError: name 'df_' is not defined

应该怎么改正呢?求助论坛高手!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2019-5-29 05:47:48
name 'df_' is not defined

倒数第二行:
df_[industry_list]=df_['secID'].apply(lambda x: get(x))

应该改为:
df_[industry_list]=df_industry['secID'].apply(lambda x: get(x))

认真点,很容易看出来

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2019-6-8 10:22:38
yunnandlg 发表于 2019-5-29 05:47
name 'df_' is not defined

倒数第二行:
好的,谢谢!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群