"Python" Pandas library Pd.to_csv operations write data and write CSV data from a CSV library __python

Source: Internet
Author: User

The

is very simple to use when data manipulation is done through the Pandas library, and then a brief instance is written to the CSV file:

In [1]: Import pandas as PD in [2]: data = {' Row1 ': [1,2,3, ' Biubiu '], ' row2 ': [3,1,3, ' Kaka ']} in [3]: Data out[3]: {' row1 ': [1, 2, 3, ' Biubiu '], ' row2 ': [3, 1, 3, ' Kaka ']} in [4]: DATA_DF = PD.

Dataframe (data) in [5]: DATA_DF out[5]: row1 row2 0 1 3 1 2 1 2 3 3 3 Biubiu Kaka           In [6]: data_df.to_csv (' data_df.csv ') in [7]: pd.read_csv (' data_df.csv ') out[7]: unnamed:0 row1 row2 0 0 1 3 1, 1 2 1 2 2 3 3 3 3 Biubiu Kaka #index表示设定是否需要行索引, set to
False indicates that no new row index is generated #header表明是否需要列索引, set to True (the default setting) indicates the need, then the column labels for DF are saved. In [ten]: Data_df.to_csv (' data_df.csv ', index=false,header=true) in [one]: pd.read_csv (' data_df.csv ') out[11]: Row1 ro W2 0 1 3 1 2 1 2 3 3 3 Biubiu kaka in [[]: Pd.read_csv (' Data_df.csv '). Index out[12]: Ran Geindex (start=0, stop=4, Step=1) in []: Pd.read_csv (' Data_df.csv '). Index.values out[13]: Array ([0, 1, 2, 3], DTYPE=INT6 4) 

with a CSV package, one line to write

in [[]: Import CSV
    ...: with open ("Test.csv", "W") as CSVFile:
    ...:     writer = csv.writer (csvfile)
    ...: c5/> ...:     #先写入columns_name ...:     writer.writerow (["Index", "A_name", "B_name"])
    ...:     # Write Multiline with writerows ...
    :     writer.writerows ([[[[0,1,3],[1,2,3],[2,3,4]])
    ...: in [M

]: Pd.read_csv (' Test.csv ')
out[15]:
   index  a_name  b_name
0      0       1       3
1       1 2       3
2      2       3       4
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.