Python 如何从系列转换为数据帧
假设我有一个简单的系列,像这样的数据框Python 如何从系列转换为数据帧,python,pandas,series,Python,Pandas,Series,假设我有一个简单的系列,像这样的数据框 S1 = Series([2.0, 0.816 , 0.2] , [51.0, 50.0 , 0.3]) 在pandas中,什么是将此系列转换为这样的数据帧的最佳方法 pd.DataFrame({ 'mean' : [2.0 , 51.0] , 'median' : [0.816 , 50.0] , 'sd' : [0.2 ,0.3] }) 这就是数据帧的外观 mean median
S1 = Series([2.0, 0.816 , 0.2] , [51.0, 50.0 , 0.3])
在pandas中,什么是将此系列转换为这样的数据帧的最佳方法
pd.DataFrame({
'mean' : [2.0 , 51.0] ,
'median' : [0.816 , 50.0] ,
'sd' : [0.2 ,0.3]
})
这就是数据帧的外观
mean median sd
2 0.816 0.2
51 50.000 0.3
一种方法
你可以做一些索引向导
D1 = S1.to_frame().reset_index().T
现在您可以将列名映射到任何位置
D1.rename( columns={0:'mean',1:'median',2:'sd'}, inplace=True) # should match the list order in S1
D1.reset_index(drop=True,inplace=True) # reset the funky index
# mean median sd
#0 51 50.000 0.3
#1 2 0.816 0.2
还有一种方法
你可以编一本字典
vars = ['mean','media','mode'] #again matching the order of the lists in S1
data_dict = dict(zip( vars,S1.iteritems()))
D1 = pandas.DataFrame.from_dict(data_dict)
一种方法
你可以做一些索引向导
D1 = S1.to_frame().reset_index().T
现在您可以将列名映射到任何位置
D1.rename( columns={0:'mean',1:'median',2:'sd'}, inplace=True) # should match the list order in S1
D1.reset_index(drop=True,inplace=True) # reset the funky index
# mean median sd
#0 51 50.000 0.3
#1 2 0.816 0.2
还有一种方法
你可以编一本字典
vars = ['mean','media','mode'] #again matching the order of the lists in S1
data_dict = dict(zip( vars,S1.iteritems()))
D1 = pandas.DataFrame.from_dict(data_dict)
在上述系列中[51.0,50.0,0.3]是指数吗?否则你能告诉我这个系列是如何创建的吗?在上面的系列中[51.0,50.0,0.3]是索引吗?否则你能告诉我这个系列是怎么创作的吗?