python中用pandas库怎样创建和读写csv文件
Admin 2022-09-13 群英技术资讯 1025 次浏览
今天小编跟大家讲解下有关“python中用pandas库怎样创建和读写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
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)
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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是python怎样获取数组元素的下标的方法。下文给大家介绍了三种实现方法,小编觉得挺实用的,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。
这篇文章主要介绍了Django2.2配置xadmin的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
内容介绍1.绘制面积图2.绘制热力图1.绘制面积图面积图常用于描述某指标随时间的变化程度。其面积也通常可以有一定的含义。绘制面积图使用的是plt.stackplot()方法。以小学时期学的常见的追击
这篇文章主要介绍了Python3 DataFrame缺失值的处理,包括缺失值的判断缺失值数据的过滤及缺失值数据的填充,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要为大家介绍了python之异步编程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008