简体   繁体   中英

Pandas: add a new column with one single value at the last row of a dataframe

My request is simple but I am stuck and do not know why... my dataframe looks like this:

   price  time  
0      1     3    
1      3     6    
2      4     7 

What I need to do is to add a new column mkt with only one value equal to 10 at the last row (in my example index = 2). What I have tried:

df.mkt=''
df.mkt.loc[-1] =10

But when I want to see again my dataframe the last row is not updated... I know the answer must be simple but I am stuck? Any idea? thanks!

You can use the at function:

df.mkt = ''
df.at[-1, 'mkt'] = 10

使用pd.Series:

df['mkt'] = [''] * (len(df)-1) +[10]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM