網(wǎng)站首頁 編程語言 正文
簡單展示如何利用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é)
原文鏈接:https://blog.csdn.net/qq_41866202/article/details/121535663
相關(guān)推薦
- 2022-07-14 如何修改numpy?array的數(shù)據(jù)類型_python
- 2022-11-28 C語言中g(shù)etchar()函數(shù)的用法小結(jié)_C 語言
- 2023-09-17 ES常見錯誤總結(jié)
- 2023-07-13 websocket的使用及nginx通信的ws代理配置
- 2022-11-12 C語言內(nèi)存操作函數(shù)使用示例梳理講解_C 語言
- 2022-05-17 Spring boot 集成Redis客戶端Lettuce,導(dǎo)致服務(wù)線程數(shù)不斷增加
- 2023-03-21 Golang使用Gin創(chuàng)建Restful?API的實現(xiàn)_Golang
- 2022-10-11 python嵌套try...except如何使用詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支