【发布时间】:2020-12-05 09:47:57
【问题描述】:
传递 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 是添加列的“正确”方式,还是可以与其他类型互换使用?
【问题讨论】:
标签: python pandas dataframe iterable