Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

怎样在dataframe中添加一列,使其值等于所在行的index值的两倍?

有一个df,想增加一列df['new']=df.index值*2
请问如何写代码,谢谢。


用map和list也可以

import pandas as pd
df=pd.DataFrame(["a","b","c"],columns=["F"])
def foo(income):
   return("income"+str(income))

df["page"]=list(map(foo,df.index))
print(df)

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你不是已经写出来了吗 - -
df['new']=df.index * 2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...