簡體   English   中英

R 中 names() function 的 python 等價物是什么

[英]What is the python equivalent for names() function in R

我在 R 中有一個代碼,其中數據框“inputdata”中有多個列,我們在整個數據框中用“old”替換單詞“new”:

names(inputData)[names(inputData)=="new"] <- "old"

我如何在 python 中執行相同的 function,我是 python 中的新手;-;

首先歡迎來到 python 的世界。試試這個https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.replace.html

例如

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                   'B': [5, 6, 7, 8, 9],
                   'C': ['a', 'b', 'c', 'd', 'e']})
df.replace(0, 5)

順便說一句,如果您只是在尋找列名更新,請執行以下操作:

df.columns = df.columns.str.replace('new', 'old') 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM