猿问

给 pandas 一个 python iterable 和 pd.Series for column

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“正确”的方式添加列,还是可以与其他类型互换使用?


青春有我
浏览 120回答 1
1回答

慕运维8079593

List在这里分配给数据框需要相同的长度对于pd.Seriesassign,它会使用index作为key去匹配original DataFrame index,然后用相同的index填充valueSeriesdf=pd.DataFrame([1,2,3],index=[9,8,7])df['New']=pd.Series([1,2,3])&nbsp;# the default index is range index , which is from 0 to n&nbsp;&nbsp;# since the dataframe index dose not match the series, then will return NaN&nbsp;dfOut[88]:&nbsp;&nbsp; &nbsp;0&nbsp; New9&nbsp; 1&nbsp; NaN8&nbsp; 2&nbsp; NaN7&nbsp; 3&nbsp; NaN具有匹配索引的不同长度df['New']=pd.Series([1,2],index=[9,8])dfOut[90]:&nbsp;&nbsp; &nbsp;0&nbsp; New9&nbsp; 1&nbsp; 1.08&nbsp; 2&nbsp; 2.07&nbsp; 3&nbsp; NaN
随时随地看视频慕课网APP

相关分类

Python
我要回答