在使用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