简体   繁体   English

从系列创建pd.Dataframe

[英]Create an pd.Dataframe from series

I have a Dataframe like this: 我有这样的Dataframe: 在此输入图像描述

then i am going to get one row with this and add a new column with an column Name time and value 15. 然后我将获得一行,并添加一个列名称时间和值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. 我现在的想法是将loc_OBL_ein转换为具有相同列名的对象,如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 谢谢你,R

It seems you need nested lists to get the row in the DataFrame from index 5: 您似乎需要嵌套列表来从索引5获取DataFrame的行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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