无效的语法错误pandas系列

时间:2017-07-08 19:25:35

标签: pandas series

我开始在jupyter笔记本上放大熊猫。在错误消息中,在=运算符下面有^,但我看不到问题。少了什么东西?谢谢!

import pandas as pd
data2 = ([1, 2, 3, 4],  index = ['a', 'b', 'c', 'd'])

s = pd.Series(data2)
print(s.shape)

这是错误:

File "<ipython-input-30-57c99bd7e494>", line 4
    data2 = ([1, 2, 3, 4],  index = ['a', 'b', 'c', 'd'])
                                  ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:1)

正确的方法是,为数据和索引分别设置变量:

import pandas as pd
data2 = [1,2,3,4]
index = ['a','b','c','d']

s = pd.Series(data2,index)
print(s.shape)

或者作为ayhan指出我们你可以用**解压缩字典:

data2 = dict(data=[1,2,3,4], index=['a','b','c','d'])
s = pd.Series(**data2)
print(s.shape)