问答详情
源自:5-2 pandas基础数据结构

在生成第三个表格时报错是怎么回事?

 df=pd.DataFrame({"A":1,"B":pd.Timestamp("20170301","C":pd.Series(1,index=list(range(4)),dtype="float32"),"D":np.array([3]*4,dtype="float32"),"E":pd.Categorical(["police","student","teacher","doctor"])})                                                       ^

SyntaxError: invalid syntax

"C":pd.Series(1,index=list(range(4)),dtype="float32"),一段显示“variables annotation cannot be combined with tuple unpacking”

提问者:慕先生4075859 2019-06-08 20:52

个回答

  • qq_金风未动蝉先觉_0
    2019-09-11 14:23:11

    建议在pycharm里编写,方便debug


    很明显你的这段代码是在这里有错:

    B":pd.Timestamp("20170301","C":

    应该修改为:

    B":pd.Timestamp("20170301"),"C":

    建议用pycharm编写,方便debug

    这一段正确的代码应该是这样的:

    df = pd.DataFrame({ "A": 1,
                        "B": pd.Timestamp("20170301"),
                        "C": pd.Series(1, index=list(range(4)), dtype="float"),
                        "D": np.array([3] * 4, dtype="float32"),
                        "E": pd.Categorical(["police", "student", "teacher", "doctor"])})