2017-04-10 4 views
1

パンダのデータフレームで2つの列をどのように平坦化しますか?パンダのデータフレームの列を整理し、元に戻す

例えば

Task 1 : 

company-asset company-debt wealth 
    GOLD   SILVER  2000.0 
    BRONZE  IRON  4000.0 
    IRON   GOLD  1500.0 

今私は(資産が+で、債務がマイナスである場合)

GOLD SILVER BRONZE IRON 
500 -2000 4000 -2500 

Task 2: 

Now i want to get the original dataframe with rows where value of 
the columns in dataframe 2 is greater than -1000 and less than +1000. 
So in the case above it will only be GOLD therefore we get this DF 

company-asset company-debt wealth 
    GOLD   SILVER  2000.0 
    IRON   GOLD  1500.0 

答えて

4

この試したい:私は何をあなたの最初のわからない

s = (df.set_index('wealth').stack() 
     .rename('metal') 
     .rename_axis(('wealth', 'type')) 
     .reset_index() 
     .pipe(lambda l: l.assign(wealth=l.wealth.where(l.type.str.endswith('asset'), 
                 -l.wealth))) 
     .groupby('metal').wealth.sum()) 
​ 
s 
#metal 
#BRONZE 4000.0 
#GOLD  500.0 
#IRON  -2500.0 
#SILVER -2000.0 
#Name: wealth, dtype: float64 

metals = s[(s > -1000) & (s < 1000)].index 
df[df['company-asset'].isin(metals) | df['company-debt'].isin(metals)] 

# company-asset company-debt wealth 
#0   GOLD   SILVER 2000.0 
#2   IRON   GOLD 1500.0 
+1

です。 –

1

を質問はです。ここで

は2番目の質問への答えである

import numpy as np 
import pandas as pd 
dd = np.array([['GOLD', 'SILVER',2000.0],['BRONZE', 'IRON', 4000.0], ['IRON', 'GOLD', 1500.0]]) 
col = ['company-asset', 'company-debt', 'wealth'] 
a = pd.DataFrame(data = dd,columns = col) 
for i in range (3): 
    a.loc[i][2] = float(a.loc[i][2]) 
a[(a['wealth']>-1000) & (a['wealth'] < 4000)] 

これは印象的である笛* *出力

Out[1]: 
    company-asset company-debt wealth 
0   GOLD  SILVER 2000 
2   IRON   GOLD 1500 
関連する問題