python教程分享如何利用python创建、读取和修改CSV数据文件

简单展示如何利用python中的pandas库创建、读取、修改csv数据文件

1 写入csv文件

import numpy as np  import pandas as pd    # -----create an initial numpy array----- #  data = np.zeros((8,4))  # print(data.dtype)  # print(type(data))  # print(data.shape)    # -----from array to dataframe----- #  df = pd.dataframe(data)  # print(type(df))  # print(df.shape)  # print(df)    # -----edit columns and index----- #  df.columns = ['a', 'b', 'c', 'd']  df.index = range(data.shape[0])  df.info()    # -----save dataframe as csv----- #  csv_save_path='./data_.csv'  df.to_csv(csv_save_path, sep=',', index=false, header=true)    # -----check----- #  df = pd.read_csv(csv_save_path)  print('-' * 25)  print(df)  

输出如下:

<class 'pandas.core.frame.dataframe'>
rangeindex: 8 entries, 0 to 7
data columns (total 4 columns):
a    8 non-null float64
b    8 non-null float64
c    8 non-null float64
d    8 non-null float64
dtypes: float64(4)
memory usage: 336.0 bytes
————————-
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0
4  0.0  0.0  0.0  0.0
5  0.0  0.0  0.0  0.0
6  0.0  0.0  0.0  0.0
7  0.0  0.0  0.0  0.0

2 读取csv文件

import pandas as pd  import numpy as np    csv_path = './data_.csv'    # -----saved as dataframe----- #  data = pd.read_csv(csv_path)  # ---if index is given in csv file, you can use next line of code to replace the previous one---  # data = pd.read_csv(csv_path, index_col=0)  print(type(data))  print(data)  print(data.shape)    # -----saved as array----- #  data_ = np.array(data)  # data_ = data.values  print(type(data_))  print(data_)  print(data_.shape)  

输出如下:

<class 'pandas.core.frame.dataframe'>
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0
4  0.0  0.0  0.0  0.0
5  0.0  0.0  0.0  0.0
6  0.0  0.0  0.0  0.0
7  0.0  0.0  0.0  0.0
(8, 4)
<class 'numpy.ndarray'>
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
(8, 4)

3 修改csv文件

import pandas as pd  import numpy as np    csv_path = './data_.csv'  df = pd.read_csv(csv_path)    # -----edit columns and index----- #  df.columns = ['x1', 'x2', 'x3', 'y']  df.index = range(df.shape[0])  # df.index = [i+1 for i in range(df.shape[0])]    # -----columns operations----- #  y = df['y']  df['x4'] = [4 for i in range(df.shape[0])]        # add  df['x5'] = [5 for i in range(df.shape[0])]  # print(df)  df.drop(columns='y', inplace=true)                # delete  # print(df)  df['x1'] = [i+1 for i in range(df.shape[0])]      # correct --(1)  # df.iloc[:df.shape[0], 0] = [i+1 for i in range(df.shape[0])]                                                    # correct --(2)  # print(df)  df['y'] = y_temp    # print(df)    # -----rows operations----- #  df.loc[df.shape[0]] = [i+2 for i in range(6)]     # add  # print(df)  df.drop(index=4, inplace=true)                    # delete  # print(df)  df.loc[0] = [i+1 for i in range(df.shape[1])]     # correct  # print(df)    # -----edit index again after rows operations!!!----- #  df.index = range(df.shape[0])    # -----save dataframe as csv----- #  csv_save_path='./data_copy.csv'  df.to_csv(csv_save_path, sep=',', index=false, header=true)    print(df)  

输出如下:

    x1   x2   x3  x4  x5    y
0  1.0  2.0  3.0   4   5  6.0
1  2.0  0.0  0.0   4   5  0.0
2  3.0  0.0  0.0   4   5  0.0
3  4.0  0.0  0.0   4   5  0.0
4  6.0  0.0  0.0   4   5  0.0
5  7.0  0.0  0.0   4   5  0.0
6  8.0  0.0  0.0   4   5  0.0
7  2.0  3.0  4.0   5   6  7.0

参考资料

csv文件的读写与修改还可以通过python的csv库来实现

总结

到此这篇关于如何利用python创建、读取和修改csv数据文件的文章就介绍到这了,更多相关python创建读取修改csv内容请搜索<猴子技术宅>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<猴子技术宅>!

需要了解更多python教程分享如何利用python创建、读取和修改CSV数据文件,都可以关注python教程分享栏目—猴子技术宅(www.ssfiction.com)

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/pythons/1091978.html

(0)
上一篇 3天前
下一篇 3天前

精彩推荐

发表评论

您的电子邮箱地址不会被公开。