List通过类型与pd.Series类型来创建新的 dataFrame 列之间有什么区别?例如,从反复试验中我注意到:
# (1d) We can also give it a Series, which is quite similar to giving it a List
df['cost1'] = pd.Series([random.choice([1.99,2.99,3.99]) for i in range(len(df))])
df['cost2'] = [random.choice([1.99,2.99,3.99]) for i in range(len(df))]
df['cost3'] = pd.Series([1,2,3]) # <== will pad length with `NaN`
df['cost4'] = [1,2,3] # <== this one will fail because not the same size
d
是否有任何其他原因pd.Series不同于传递标准 python 列表?数据框可以采用任何 python 可迭代对象还是对可以传递给它的内容有限制?最后,是使用pd.Series“正确”的方式添加列,还是可以与其他类型互换使用?
慕运维8079593
相关分类