pandas小记:pandas基本设置

http://blog.csdn.net/pipisorry/article/details/49519545

安装

Bugfix
from pandas.core.groupby.groupby import DataFrameGroupBy, \ E ImportError: cannot import name 'DataFrameGroupBy'
解决:
pip install tqdm --upgrade
[Pandas not working: DataFrameGroupBy ; PanelGroupBy]

 

pandas输出格式设置示例

pandas输出精度局部设置local setting

with pd.option_context('display.precision', 3):
    print(df)

Note: 试了好久终于找到了这种设置方法!

它是这样实现的

class option_context(object):
    """
    >>> with option_context('display.max_rows', 10, 'display.max_columns', 5):

    """

    def __init__(self, *args):
        if not (len(args) % 2 == 0 and len(args) >= 2):
            raise ValueError(
                'Need to invoke as'
                'option_context(pat, val, [(pat, val), ...)).'
            )

        self.ops = list(zip(args[::2], args[1::2]))

    def __enter__(self):
        undo = []
        for pat, val in self.ops:
            undo.append((pat, _get_option(pat, silent=True)))

        self.undo = undo

        for pat, val in self.ops:
            _set_option(pat, val, silent=True)

    def __exit__(self, *args):
        if self.undo:
            for pat, val in self.undo:
                _set_option(pat, val, silent=True)

其实类似numpy数组输出精度的设置:[numpy输入输出]

pandas浮点数据输出禁用科学计数法的方式

with pd.option_context('display.float_format', lambda x: '%.3f' % x)

当然series也可以这样

Series(np.random.randn(3)).apply(lambda x: '%.3f' % x)

皮皮blog

 

pandas选项和设置

List of Options

OptionDefaultFunction
display.chop_thresholdNoneIf set to a float value, all floatvalues smaller then the giventhreshold will be displayed asexactly 0 by repr and friends.
display.colheader_justifyrightControls the justification ofcolumn headers. used by DataFrameFormatter.
display.column_space12No description available.
display.date_dayfirstFalseWhen True, prints and parses dateswith the day first, eg 20/01/2005
display.date_yearfirstFalseWhen True, prints and parses dateswith the year first, eg 2005/01/20
display.encodingUTF-8Defaults to the detected encodingof the console. Specifies the encodingto be used for strings returned byto_string, these are generally stringsmeant to be displayed on the console.
display.expand_frame_reprTrueWhether to print out the full DataFramerepr for wide DataFrames acrossmultiple lines,max_columns isstill respected, but the output willwrap-around across multiple “pages”if its width exceedsdisplay.width.
display.float_formatNoneThe callable should accept a floatingpoint number and return a string withthe desired format of the number.This is used in some places likeSeriesFormatter.See core.format.EngFormatter for an example.
display.height60Deprecated. Use display.max_rows instead.
display.large_reprtruncateFor DataFrames exceeding max_rows/max_cols,the repr (and HTML repr) can showa truncated table (the default from 0.13),or switch to the view from df.info()(the behaviour in earlier versions of pandas).allowable settings, [‘truncate’, ‘info’]
display.line_width80Deprecated. Use display.width instead.
display.max_columns20max_rows and max_columns are usedin __repr__() methods to decide ifto_string() or info() is used torender an object to a string. Incase python/IPython is running ina terminal this can be set to 0 andpandas will correctly auto-detectthe width the terminal and swap toa smaller format in case all columnswould not fit vertically. The IPythonnotebook, IPython qtconsole, or IDLEdo not run in a terminal and henceit is not possible to do correctauto-detection. ‘None’ value meansunlimited.
display.max_colwidth50The maximum width in characters ofa column in the repr of a pandasdata structure. When the column overflows,a ”...” placeholder is embedded inthe output.
display.max_info_columns100max_info_columns is used in DataFrame.infomethod to decide if per column informationwill be printed.
display.max_info_rows1690785df.info() will usually show null-countsfor each column. For large framesthis can be quite slow. max_info_rowsand max_info_cols limit this nullcheck only to frames with smallerdimensions then specified.
display.max_rows60This sets the maximum number of rowspandas should output when printingout various output. For example,this value determines whether therepr() for a dataframe prints outfully or just a summary repr.‘None’ value means unlimited.
display.max_seq_items100when pretty-printing a long sequence,no more then max_seq_items willbe printed. If items are omitted,they will be denoted by the additionof ”...” to the resulting string.If set to None, the number of itemsto be printed is unlimited.
display.memory_usageTrueThis specifies if the memory usage ofa DataFrame should be displayed when thedf.info() method is invoked.
display.mpl_styleNoneSetting this to ‘default’ will modifythe rcParams used by matplotlibto give plots a more pleasing visualstyle by default. Setting this toNone/False restores the values totheir initial value.
display.multi_sparseTrue“Sparsify” MultiIndex display (don’tdisplay repeated elements in outerlevels within groups)
display.notebook_repr_htmlTrueWhen True, IPython notebook willuse html representation forpandas objects (if it is available).
display.pprint_nest_depth3Controls the number of nested levelsto process when pretty-printing
display.precision6Floating point output precision interms of number of places after thedecimal, for regular formatting as wellas scientific notation. Similar tonumpy’sprecision print option
display.show_dimensionstruncateWhether to print out dimensionsat the end of DataFrame repr.If ‘truncate’ is specified, onlyprint out the dimensions if theframe is truncated (e.g. not displayall rows and/or columns)
display.width80Width of the display in characters.In case python/IPython is running ina terminal this can be set to Noneand pandas will correctly auto-detectthe width. Note that the IPython notebook,IPython qtconsole, or IDLE do not run in aterminal and hence it is not possibleto correctly detect the width.
io.excel.xls.writerxlwtThe default Excel writer engine for‘xls’ files.
io.excel.xlsm.writeropenpyxlThe default Excel writer engine for‘xlsm’ files. Available options:‘openpyxl’ (the default).
io.excel.xlsx.writeropenpyxlThe default Excel writer engine for‘xlsx’ files.
io.hdf.default_formatNonedefault format writing format, ifNone, then put will default to‘fixed’ and append will default to‘table’
io.hdf.dropna_tableTruedrop ALL nan rows when appendingto a table
mode.chained_assignmentwarnRaise an exception, warn, or noaction if trying to use chainedassignment, The default is warn
mode.sim_interactiveFalseWhether to simulate interactive modefor purposes of testing
mode.use_inf_as_nullFalseTrue means treat None, NaN, -INF,INF as null (old way), False meansNone and NaN are null, but INF, -INFare not null (new way).

[Options and Settings]

 

pandas.set_option

pandas.set_option(pat,value) = <pandas.core.config.CallableDynamicDoc object at 0xb559f20c>

Sets the value of the specified option.

示例

[Python pandas, widen output display?]

 

to_csv精度设置

df_data.to_csv(outfile, index=False,
                   header=False, float_format='%11.6f')

[Print different precision by column with pandas.DataFrame.to_csv()]

from:http://blog.csdn.net/pipisorry/article/details/49519545

ref:pandas.core.config.get_option

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值