欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2022-07-04 14:07:08|栏目:Python代码|点击:

简单展示如何利用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 ChainMap的使用和说明详解

栏    目:Python代码

下一篇:Python中引用传参四种方式介绍

本文标题:如何利用python创建、读取和修改CSV数据文件

本文地址:http://www.codeinn.net/misctech/206812.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有