将Pandas Dataframe从Row转换为Columnar

时间:2016-09-17 18:10:14

标签: python sql pandas pyspark spark-dataframe

My Dataframe(df)如下所示:

Error: instruction cannot be conditional -- `vst1ne.u8 {d12},[outI]!'

我希望数据框如下所示:

    vcmp.f64 d12, #0
    vmrs APSR_nzcv, fpscr
    beq .jumpover
    vst1.u8 {d12}, [outI]!
.jumpover:

我厌倦了pandas中的df.pivot,我可以提供单值列。它不需要多于一个。当我提供不止一个时,我得到以下异常。 pandas_pivot

Date       FieldA  ValueA ValueB
09-02-2016 TypeA   3       5
09-02-2016 TypeB   6       7

3 个答案:

答案 0 :(得分:2)

df1 = df.set_index(['Date', 'FieldA']).unstack()
df1.columns = df1.columns.map('_'.join)

df1.reset_index()

enter image description here

设置参考

from StringIO import StringIO
import pandas as pd

text = """Date       FieldA  ValueA ValueB
09-02-2016 TypeA   3       5
09-02-2016 TypeB   6       7"""

df = pd.read_csv(StringIO(text), delim_whitespace=True)

df

enter image description here

答案 1 :(得分:0)

In [36]: df
Out[36]: 
        Date FieldA  ValueA  ValueB
0 2016-09-02  TypeA       3       5
1 2016-09-02  TypeB       6       7
2 2016-09-03  TypeA       4       8
3 2016-09-03  TypeB       3       9

In [37]: v_cols = df.columns.difference(['FieldA', 'Date'])

In [38]: def func(x):
     ...:     d = {'_'.join([t, c]): x[x['FieldA'] == t][c].iloc[0] for t in x.FieldA for c in v_cols}
     ...:     for k, v in d.iteritems():
     ...:         x[k] = v
     ...:     return x
     ...: 

In [39]: newdf = df.groupby('Date').apply(func)

In [40]: newdf.drop(v_cols.tolist() + ['FieldA'], axis=1).drop_duplicates()
Out[340]: 
        Date  TypeA_ValueA  TypeA_ValueB  TypeB_ValueA  TypeB_ValueB
0 2016-09-02             3             5             6             7
2 2016-09-03             4             8             3             9

答案 2 :(得分:0)

使用 match "*path", :to => proc {|env| [200, { 'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Credentials' => 'true', 'Access-Control-Request-Method' => '*', 'Access-Control-Allow-Headers' => 'Origin, X-Requested-With, Content-Type, Accept, Authorization', 'Content-Type' => 'text/plain' }, ["CORS Preflight"]] }, :via => [:options]

pd.pivot_table

因此,您将获得具有MultiIndex的DataFrame。如果要将其展平并在列名中使用In [1]: pd.pivot_table(df, index='Date', columns='FieldA', values=['ValueA', 'ValueB']) Out[1]: ValueA ValueB FieldA TypeA TypeB TypeA TypeB Date 09-02-2016 3 6 5 7 作为分隔符,则可以执行以下操作:

_