Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How NumPy and pandas operate on CSV files

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces NumPy and pandas how to operate the CSV file related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this NumPy and pandas how to operate the CSV file article will have a harvest, let's take a look at it.

The array is stored as a segregated file such as CSV:

Set the value of an array element to NaN:

In [26]: import numpy as np In [27]: np.random.seed (42) In [28]: a = np.random.randn (3Power4) In [29]: a [2] [2] = np.nan In [30]: print (a) [[0.49671415-0.1382643 0.64768854 1.52302986] [- 0.23415337-0.23413696 1.57921282 0.76743473] [- 0.46947439 0.54256004 nan-0.46572975]]

The savetxt () function of NumPy is a function corresponding to loadtxt (), which saves arrays in a partitioned file format such as CSV:

In [31]: np.savetxt ('np.csv',a,fmt='%.2f',delimiter=',',header= "# 1, 2, 3, 4")

In the function call above, we specify the name of the file to hold the array, the array, the optional format, the spacer, and an optional title

Through cat np.csv, you can view the specific contents of the np.csv file you just created.

Use random arrays to create pandas DataFrame:

In [38]: df = pd.DataFrame (a) In [39]: df Out [39]: 01230 0.496714-0.138264 0.647689 1.523030 1-0.234153-0.234137 1.579213 0.767435 2-0.469474 0.542560 NaN-0.465730

Pandas will automatically name the column for our data.

Using the to_csv () method of pandas, you can generate a DataFrame for the CSV file:

In [40]: df.to_csv ('pd.csv',float_format='%.2f',na_rep= "NAN!")

For this method, we need to provide a file name, an optional format string for the formatting parameters of the savetxt () function similar to NumPy, and an optional string that represents NaN

This is the end of the article on "how NumPy and pandas manipulate CSV files". Thank you for reading! I believe you all have a certain understanding of "how NumPy and pandas manipulate CSV files". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report