如何格式化我的pandas dataFrame输出?

时间:2018-01-17 20:17:13

标签: python pandas

这是我的代码

from pandas_datareader import data as pdr
import datetime 
import fix_yahoo_finance as yf
yf.pdr_override() # <== that's all it takes :-)   

start = datetime.datetime(2008, 1, 1)
end = datetime.datetime(2018, 1, 1)
data = yf.download("CGG", start, end)

print (type(data))

data.to_csv('cgg.csv', sep='\t')

我正在使用yahoo修复程序来获取雅虎数据。这些是来自我的csv文件的50行

head -50 cgg.csv 
Date    Open    High    Low Close   Adj Close   Volume
2007-12-31  1828.160034 1828.47998  1793.5999760000002  1793.5999760000002  1339.275269 2100
2008-01-02  1821.119995 1840.319946 1801.5999760000002  1819.52002  1358.629639 5100
2008-01-03  1844.160034 1871.359985 1841.280029 1857.9200440000002  1387.302856 6100
2008-01-04  1859.199951 1861.439941 1783.040039 1806.0799559999998  1348.5939939999998  12600
2008-01-07  1793.5999760000002  1795.839966 1744.319946 1767.680054 1319.920898 6700
2008-01-08  1786.880005 1808.319946 1758.0799559999998  1762.880005 1316.336792 6000
2008-01-09  1775.040039 1812.47998  1772.47998  1804.800049 1347.638306 10800
2008-01-10  1681.9200440000002  1744.0  1676.800049 1730.2399899999998  1291.9646   19900
2008-01-11  1626.560059 1656.319946 1619.839966 1643.839966 1227.449951 25700
2008-01-14  1691.52002  1733.119995 1650.880005 1720.0  1284.3183589999999  9900
2008-01-15  1652.47998  1665.280029 1593.280029 1603.52002  1197.3432619999999  14400
2008-01-16  1555.199951 1600.319946 1533.439941 1547.199951 1155.2891849999999  13300
2008-01-17  1620.47998  1641.599976 1553.280029 1567.359985 1170.342651 40200
2008-01-18  1590.400024 1590.400024 1504.0  1540.47998  1150.271362 25100
2008-01-22  1449.599976 1470.079956 1344.0  1462.719971 1092.208252 13600
2008-01-23  1339.199951 1409.920044 1312.0  1406.079956 1049.915283 16900
2008-01-24  1427.52002  1479.359985 1415.680054 1477.439941 1103.199585 12000
2008-01-25  1534.079956 1544.319946 1474.23999  1484.800049 1108.6953130000002  10900
2008-01-28  1500.47998  1528.0  1464.319946 1528.0  1140.9526369999999  7200
2008-01-29  1555.52002  1560.959961 1531.839966 1549.439941 1156.961792 9600
2008-01-30  1509.76001  1552.0  1496.0  1513.599976 1130.200195 7200
2008-01-31  1424.319946 1503.680054 1424.319946 1484.800049 1108.6953130000002  9500
2008-02-01  1496.640015 1539.52002  1494.079956 1538.23999  1148.598755 5200
2008-02-04  1553.920044 1556.800049 1537.280029 1540.160034 1150.032471 7300
2008-02-05  1504.959961 1507.839966 1475.52002  1477.76001  1103.438599 9000
2008-02-06  1445.76001  1457.280029 1422.079956 1423.359985 1062.818237 9400
2008-02-07  1412.160034 1431.040039 1393.920044 1418.560059 1059.2341310000002  7500
2008-02-08  1408.0  1424.319946 1400.0  1419.839966 1060.189941 13800
2008-02-11  1429.119995 1429.119995 1399.040039 1424.319946 1063.535034 8200
2008-02-12  1466.880005 1490.560059 1442.23999  1458.560059 1089.102051 9100
2008-02-13  1511.040039 1548.160034 1504.319946 1544.319946 1153.138672 7000
2008-02-14  1522.880005 1522.880005 1471.359985 1471.359985 1098.659668 9100
2008-02-15  1507.839966 1507.839966 1458.23999  1478.719971 1104.155396 9900
2008-02-19  1546.880005 1557.76001  1527.359985 1537.280029 1147.881958 6200
2008-02-20  1548.160034 1587.839966 1544.0  1578.560059 1178.705688 7100
2008-02-21  1657.280029 1661.119995 1598.400024 1603.199951 1197.104126 16100
2008-02-22  1619.199951 1634.23999  1599.040039 1633.599976 1219.803711 9700
2008-02-25  1647.359985 1687.040039 1633.280029 1682.2399899999998  1256.123047 5700
2008-02-26  1664.640015 1707.839966 1654.400024 1704.959961 1273.088013 8600
2008-02-27  1687.040039 1720.959961 1675.52002  1704.640015 1272.8491210000002  9800
2008-02-28  1632.959961 1634.560059 1577.920044 1588.800049 1186.351807 19400
2008-02-29  1577.920044 1587.839966 1555.839966 1555.839966 1161.740601 7800
2008-03-03  1534.719971 1547.839966 1516.800049 1540.800049 1150.510376 7100
2008-03-04  1527.040039 1536.640015 1508.160034 1532.47998  1144.297852 16400
2008-03-05  1543.680054 1596.47998  1543.680054 1592.640015 1189.219116 9500
2008-03-06  1601.920044 1601.920044 1550.400024 1552.640015 1159.351196 7000
2008-03-07  1491.839966 1511.680054 1467.199951 1483.199951 1107.50061  6400
2008-03-10  1485.76001  1502.079956 1448.959961 1458.880005 1089.340942 6800
2008-03-11  1552.640015 1560.319946 1520.959961 1555.199951 1161.262817 9700

如何格式化五个浮点列并保留第一个和最后一个未更改?类似

pd.options.display.float_format = '${:,.2f}'.format

除了第一个和最后一个。如何将这些浮点数舍入到两个小数位? 如果我尝试@shrumm建议我得到

Date    Open    High    Low Close   Adj Close   Volume
2007-12-31  $1,828.16   $1,828.48   $1,793.60   $1,793.60   $1,339.28   2100
2008-01-02  $1,821.12   $1,840.32   $1,801.60   $1,819.52   $1,358.63   5100
2008-01-03  $1,844.16   $1,871.36   $1,841.28   $1,857.92   $1,387.30   6100
2008-01-04  $1,859.20   $1,861.44   $1,783.04   $1,806.08   $1,348.59   12600

我想摆脱$ sign。

1 个答案:

答案 0 :(得分:2)

你应该可以做到

df['Open'] = df['Open'].map('{:,.2f}'.format)
df['High'] = df['High'].map('{:,.2f}'.format)
df['Low'] = df['Low'].map('{:,.2f}'.format)
df['Close'] = df['Close'].map('{:,.2f}'.format)
df['Adj Close'] = df['Adj Close'].map('{:,.2f}'.format)

相关问题:How to display pandas DataFrame of floats using a format string for columns?