2017-05-31 143 views
-1

我有一個熊貓數據幀,它看起來像:Python的熊貓表操作

broker1 broker2 broker3 ticker 
0 val1  val2  val3  tick1 
1 val4  None  val6  tick2 

我會想操縱它(不知道什麼是這個術語:支點扭轉GROUPBY?)的方式,使得他們是按其個人價值排序,即:

ticker ticker_b broker 
0 tick1 val1  broker1 
1 tick1 val2  broker2 
2 tick1 val3  broker3 
3 tick2 val4  broker1 
4 tick2 val6  broker2 

有沒有辦法使用熊貓來做到這一點?非常感謝您提供的任何幫助!

回答

2

使用melt(),其中 「unpivots」 的表:

In [46]: df = pd.read_table(io.StringIO(""" broker1 broker2 broker3 ticker 
    ...: 0 val1  val2  val3  tick1 
    ...: 1 val4  None  val6  tick2"""), sep='\s+') 

In [47]: df = df.replace('None', float('nan')) 

In [48]: melted = df.melt(id_vars=("ticker",), 
    ...:     var_name="broker", 
    ...:     value_name="ticker_b").\ 
    ...:  dropna().\ 
    ...:  sort_values(["ticker", "ticker_b"]) 

In [49]: melted 
Out[49]: 
    ticker broker ticker_b 
0 tick1 broker1  val1 
2 tick1 broker2  val2 
4 tick1 broker3  val3 
1 tick2 broker1  val4 
5 tick2 broker3  val6 
+0

太感謝你了! –

0

而且管理使用concat它做,雖然ILJA Everila的回答是更好:

oldcols = ["broker1","broker2","broker3"] 
newcols = ["ticker","ticker_b","broker"] 
newdf = pd.DataFrame(columns=newcols) 
for col in oldcols: 
    tmpdf = df.loc[:,["ticker",col]] 
    tmpdf.columns = newcols[0:2] 
    tmpdf[newcols[-1]] = col 
    newdf = pd.concat([newdf,tmpdf]) 
newdf.dropna(subset=["ticker"], inplace=True)