如何利用python創(chuàng)建、讀取和修改CSV數(shù)據(jù)文件
簡單展示如何利用python中的pandas庫創(chuàng)建、讀取、修改CSV數(shù)據(jù)文件
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庫來實現(xiàn)
python中csv文件的創(chuàng)建、讀取、修改等操作總結(jié)
總結(jié)
到此這篇關于如何利用python創(chuàng)建、讀取和修改CSV數(shù)據(jù)文件的文章就介紹到這了,更多相關python創(chuàng)建讀取修改CSV內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python?nonlocal關鍵字?與?global?關鍵字解析
這篇文章主要介紹了Python?nonlocal關鍵字?與?global?關鍵字解析,nonlocal關鍵字用來在函數(shù)或其他作用域中使用外層變量,global關鍵字用來在函數(shù)或其他局部作用域中使用全局變量,更多香瓜內(nèi)容需要的小伙伴可以參考一下2022-03-03python中的位置參數(shù)和關鍵字參數(shù)詳解
位置參數(shù)和關鍵字參數(shù)是 Python 中的兩種不同類型的函數(shù)參數(shù)傳遞方式,位置參數(shù)依賴于參數(shù)的位置順序,而關鍵字參數(shù)通過參數(shù)名傳遞,不受位置影響,本文通過代碼示例給大家詳細介紹了python中的位置參數(shù)和關鍵字參數(shù),需要的朋友可以參考下2023-12-12Tensorflow實現(xiàn)卷積神經(jīng)網(wǎng)絡用于人臉關鍵點識別
這篇文章主要介紹了Tensorflow實現(xiàn)卷積神經(jīng)網(wǎng)絡用于人臉關鍵點識別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03pytorch VGG11識別cifar10數(shù)據(jù)集(訓練+預測單張輸入圖片操作)
這篇文章主要介紹了pytorch VGG11識別cifar10數(shù)據(jù)集(訓練+預測單張輸入圖片操作),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python實現(xiàn)批量梯度下降法(BGD)擬合曲線
這篇文章主要介紹了Python實現(xiàn)批量梯度下降法(BGD)擬合曲線,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04