raffa_sa
raffa_sa

Reputation: 425

Create an pd.Dataframe from series

I have a Dataframe like this: enter image description here

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

Upvotes: 3

Views: 110

Answers (1)

jezrael
jezrael

Reputation: 863751

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

Upvotes: 4

Related Questions