swn.file.write_formatted_frame#
- swn.file.write_formatted_frame(df, fname, index=True, comment_header=True)#
Write a data frame as a free formatted table.
- Parameters:
- dfpandas.DataFrame
DataFrame to write.
- fnamestr, path-like or file-like object
Path to write file.
- indexbool, default True
Write row names (index).
- comment_headerbool, default True
If True, first line starts with ‘#’ to make it a comment, otherwise it will be an uncommented header.
See also
Examples
>>> from io import StringIO >>> import pandas as pd >>> from swn.file import write_formatted_frame >>> df = pd.DataFrame({ ... "value1": [-1e10, -1e-10, 0, 1e-10, 1, 1000], ... "value2": [1, 10, 100, 1000, 10000, 100000], ... "value3": ["first one", "two", "three", None, "five", "six"], ... }, index=[1, 12, 33, 40, 450, 6267]) >>> df.index.name = "ifno" >>> print(df) value1 value2 value3 ifno 1 -1.000000e+10 1 first one 12 -1.000000e-10 10 two 33 0.000000e+00 100 three 40 1.000000e-10 1000 None 450 1.000000e+00 10000 five 6267 1.000000e+03 100000 six >>> f = StringIO() >>> write_formatted_frame(df, f) >>> _ = f.seek(0) >>> print(f.read(), end="") # ifno value1 value2 value3 1 -1.000000e+10 1 'first one' 12 -1.000000e-10 10 two 33 0.000000e+00 100 three 40 1.000000e-10 1000 450 1.000000e+00 10000 five 6267 1.000000e+03 100000 six