在使用Pandas DataFrames时,将新列添加到现有dataframe
, 'b':[0.101208,-0.243316,0.075793], 'c':[-0.181532,0.051767,-0.451460], 'D':[0.241273,1.577318,-0.012493] })) #添加一个带有随机值的新列“ e” slength = len(df1 ['a']) df1 = df1.sign(e = pd.series(np.random.randn(slength))。值) 2。使用loc [row_index,col_indexer] = value:#使用loc添加新列'f' df1.loc [:,'f'] = pd.series(np.random.randn(slength),index = df1.index)
3。使用df [new_column_name] = pd.series(values,index = df.index):
#使用旧方法添加新列'g' df1 ['g'] = pd.series(np.random.randn(slength),index = df1.index)请记住,后者的方法可能会触发pandas的新版本。通常建议使用分配或LOC来提高效率和清晰度。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3