python - Pandas df.itertuples renaming dataframe columns when printing -


i know pandas' itertuples() return values of each including column names follows:

ab=pd.dataframe(np.random.random([3,3]),columns=['hi','low','med']) in ab.itertuples():     print(i) 

and output follows:

pandas(index=0, hi=0.05421443, low=0.2456833, med=0.491185) pandas(index=1, hi=0.28670429, low=0.5828551, med=0.279305) pandas(index=2, hi=0.53869406, low=0.3427290, med=0.750075) 

however, have no idea why doesn't shows columns expected set of code below:

            qqq equity  spy equity date                                     2017-06-19            0.0            1.0 2017-06-20            0.0           -1.0 2017-06-21            0.0            0.0 2017-06-22            0.0            0.0 2017-06-23            1.0            0.0 2017-06-26            0.0            0.0 2017-06-27           -1.0            0.0 2017-06-28            1.0            0.0 2017-06-29           -1.0            0.0 2017-06-30            0.0            0.0 

the above pandas dataframe timestamp index, float64 values in list, , list of string ['us qqq equity','us spy equity'] columns.

when this:

for row in data.itertuples():     print (row) 

it shows columns _1 , _2 follows:

pandas(index=timestamp('2017-06-19 00:00:00'), _1=0.0, _2=1.0) pandas(index=timestamp('2017-06-20 00:00:00'), _1=0.0, _2=-1.0) pandas(index=timestamp('2017-06-21 00:00:00'), _1=0.0, _2=0.0) pandas(index=timestamp('2017-06-22 00:00:00'), _1=0.0, _2=0.0) pandas(index=timestamp('2017-06-23 00:00:00'), _1=1.0, _2=0.0) pandas(index=timestamp('2017-06-26 00:00:00'), _1=0.0, _2=0.0) pandas(index=timestamp('2017-06-27 00:00:00'), _1=-1.0, _2=0.0) pandas(index=timestamp('2017-06-28 00:00:00'), _1=1.0, _2=0.0) pandas(index=timestamp('2017-06-29 00:00:00'), _1=-1.0, _2=0.0) pandas(index=timestamp('2017-06-30 00:00:00'), _1=0.0, _2=0.0) 

does has clue have done wrong? have variable referencing issue when creating original dataframe? (also, side question, learnt community type of data generated itertuples() should tuples, seems (as shown above), return type verified type statement?)

thank patience still trying master application of dataframe.

this seems issue handling column names having spaces in them. if replace column names different ones without spaces, work:

in [732]: df.columns = ['us_qqq_equity', 'us_spy_equity']  in [733]: r in df.head().itertuples():      ...:     print(r)      ...:      pandas(index='2017-06-19', us_qqq_equity=0.0, us_spy_equity=1.0) pandas(index='2017-06-20', us_qqq_equity=0.0, us_spy_equity=-1.0) pandas(index='2017-06-21', us_qqq_equity=0.0, us_spy_equity=0.0) pandas(index='2017-06-22', us_qqq_equity=0.0, us_spy_equity=0.0) pandas(index='2017-06-23', us_qqq_equity=1.0, us_spy_equity=0.0) 

column names spaces cannot represented in named tuples, renamed automatically when printing.


a more flexible manner of replacing spaces in columns names (courtesy maxu) using df.columns.str.replace:

df.columns = df.columns.str.replace(r'\s+', '_') 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -