@@ -1568,6 +1568,83 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
1568
1568
file.
1569
1569
pandas.read_csv : Read a comma-separated values (CSV) file into
1570
1570
a DataFrame.
1571
+
1572
+ Examples
1573
+ --------
1574
+ Setup:
1575
+
1576
+ >>> from csv import reader
1577
+ >>> from tempfile import TemporaryFile
1578
+ >>> def print_helper(temp):
1579
+ ... # Read and print a "raw" version of the input file
1580
+ ... # "Rewind" to the begining of the file
1581
+ ... _ = temp.seek(0)
1582
+ ... r = reader(temp, delimiter='X')
1583
+ ... for row in r:
1584
+ ... print(''.join(row))
1585
+
1586
+ A simple example of writing (and reading) a CSV file:
1587
+
1588
+ >>> df = pd.DataFrame({'col_a': [1, 2], 'col_b': [9, 8]},
1589
+ ... index=['a','b'])
1590
+ >>> df
1591
+ col_a col_b
1592
+ a 1 9
1593
+ b 2 8
1594
+ >>> with TemporaryFile('w+') as temp:
1595
+ ... df.to_csv(temp)
1596
+ ... _ = temp.seek(0)
1597
+ ... df_out = pd.read_csv(temp, sep=',', index_col=0)
1598
+ ... print_helper(temp)
1599
+ ,col_a,col_b
1600
+ a,1,9
1601
+ b,2,8
1602
+ >>> df_out
1603
+ col_a col_b
1604
+ a 1 9
1605
+ b 2 8
1606
+
1607
+ Assert equality ignoring `dtype`
1608
+
1609
+ >>> pd.testing.assert_frame_equal(df, df_out, check_dtype=False)
1610
+
1611
+ **Custom formatting**
1612
+
1613
+ Write a CSV file with a custom separator, missing value representation,
1614
+ and float and dates formatting:
1615
+
1616
+ >>> df = pd.DataFrame({
1617
+ ... 'col_a': [1.0, 2.0],
1618
+ ... 'col_b': [0.0001, 0.01],
1619
+ ... 'date_col': pd.date_range('2018-03-10', '2018-03-11')
1620
+ ... })
1621
+ >>> df.iloc[0,0] = np.nan
1622
+ >>> df
1623
+ col_a col_b date_col
1624
+ 0 NaN 0.0001 2018-03-10
1625
+ 1 2.0 0.0100 2018-03-11
1626
+ >>> with TemporaryFile('w+') as temp:
1627
+ ... df.to_csv(temp, sep=':', na_rep='NaNa', float_format='%.2f',
1628
+ ... date_format='%Y/%m/%d')
1629
+ ... _ = temp.seek(0)
1630
+ ... df_out = pd.read_csv(temp, sep=':', na_values='NaNa',
1631
+ ... index_col=0, parse_dates=['date_col'])
1632
+ ... print_helper(temp)
1633
+ :col_a:col_b:date_col
1634
+ 0:NaNa:0.00:2018/03/10
1635
+ 1:2.00:0.01:2018/03/11
1636
+
1637
+ Note the "standard" Python NaN representation "NaN"
1638
+
1639
+ >>> df_out
1640
+ col_a col_b date_col
1641
+ 0 NaN 0.00 2018-03-10
1642
+ 1 2.0 0.01 2018-03-11
1643
+
1644
+ Assert equality with a rounded column to match the format used
1645
+
1646
+ >>> df['col_b'] = np.round(df.col_b, 2)
1647
+ >>> pd.testing.assert_frame_equal(df, df_out, check_dtype=False)
1571
1648
"""
1572
1649
1573
1650
if tupleize_cols is not None :
0 commit comments