如何将数据框行调整为列

时间:2015-08-25 13:32:27

标签: python pandas dataframe

import pandas as pd
import pandas.io.data as web
from pandas import Series, DataFrame
import matplotlib
import matplotlib.pyplot as plt
from numpy.random import randn
import numpy as np
matplotlib.style.use('ggplot')

stocks = {'xom': '2014-01-01', 'dvn': '2013-01-01', 'aapl': '2013-01-01'}

L = dict()

for stock, date in stocks.items():
    price = web.get_data_yahoo(stock, date)['Adj Close']
    change = price.diff().cumsum()
    perChange = change / price.iloc[0]
    L[stock] = perChange

df = pd.concat(L, axis=1)

df2 = df.describe()

如何格式化df2以使列为min,max,std等...并且行是股票代码?

2 个答案:

答案 0 :(得分:2)

使用日期框架的转置:DataFrame.T

df2 = df.describe().T # this is the equivalent of df.describe().transpose()

print df2
      count      mean       std       min       25%       50%       75%       max  
aapl    665  0.195720  0.331271 -0.284546 -0.089219  0.110605  0.501857  0.783157 
dvn     665  0.202538  0.143291 -0.246586  0.104409  0.175463  0.286709  0.548577  
xom     413 -0.049164  0.062285 -0.273573 -0.096234 -0.045035 -0.001124  0.060982

答案 1 :(得分:1)

您要添加;

df2 = df2.transpose()