简体   繁体   中英

Create an pd.Dataframe from series

I have a Dataframe like this: 在此输入图像描述

then i am going to get one row with this and add a new column with an column Name time and value 15.

loc_OBL_ein = df.loc[5]
loc_OBL_ein.insert(1,'time',value=15) 

then i get an error 'Series' object has no attribute 'insert'. My idea now was to convert loc_OBL_ein into an object with the same column names like df. How can I do that?
Or is there another way to get this one particular row and keep the object format?

Thank you, R

It seems you need nested lists to get the row in the DataFrame from index 5:

df = pd.DataFrame({
        'A':list('abcdef'),
         'B':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
         'D':[1,3,5,7,1,0],
         'E':[5,3,6,9,2,4],
         'F':list('aaabbb')
})

print (df)
   A  B  C  D  E  F
0  a  4  7  1  5  a
1  b  5  8  3  3  a
2  c  4  9  5  6  a
3  d  5  4  7  9  b
4  e  5  2  1  2  b
5  f  4  3  0  4  b

loc_OBL_ein = df.loc[[5]]
loc_OBL_ein.insert(1,'time',value=15) 
print (loc_OBL_ein)
   A  time  B  C  D  E  F
5  f    15  4  3  0  4  b

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