Pandas小灶~

本文详细介绍了Pandas DataFrame的创建、插入行、数据访问及处理等操作,包括DataFrame的append、insert、concat方法,以及行列倒置、读写文件、查看和删除数据的技巧。
摘要由CSDN通过智能技术生成

目录

一、DataFrame的创建

二、插入行

1️⃣append

Parameters

Returns

See Also

Notes

2️⃣insert

Parameters

See Also

3️⃣contact 合并数据 

Returns

三、数据的访问

四、数据的处理 

1️⃣行列倒置————df.T

2️⃣读入和写入文件 

3️⃣查看数据 

4️⃣删除数据 drop() 

Parameters

Returns


一、DataFrame的创建

需要注意的是DataFrame的数据源必须要是二位数组(字典就是一种二维数组)

例如:

import pandas
dic = {'Name':['Jack','Rose','Lucy'],'Score':[100,93,94],"Gender":["Male","Female","Female"]}
df = pandas.DataFrame(dic)

下面是DataFrame函数的参数: 

data : ndarray (structured or homogeneous), Iterable, dict, or DataFrame
    Dict can contain Series, arrays, constants, dataclass or list-like objects. If data is a dict, column order follows insertion-order. If a dict contains Series which have an index defined, it is aligned by its index.

index : Index or array-like
    Index to use for resulting frame. Will default to RangeIndex if no indexing information part of input data and no index provided.
columns : Index or array-like

        Column labels to use for resulting frame when data does not have them, defaulting to RangeIndex(0, 1, 2, ..., n). If data contains column labels, will perform column selection instead.
dtype : dtype, default None
    Data type to force. Only a single dtype is allowed. If None, infer.
copy : bool or None, default None
    Copy data from inputs. For dict data, the default of None behaves like copy=True. For DataFrame or 2d ndarray input, the default of None behaves like copy=False.

 其中index指最左边一列的编号,column指最上面一排的编号,如果不设置都是默认0~n-1。最后一个参数则是是否从输入中复制,为一个布尔值,一般不会用到。

如果要修改列的编号,代码如下:

import pandas
dic = {'Name':['Jack','Rose','Lucy'],'Score':[100,93,94],"Gender":["Male","Female","Female"]}
df = pandas.DataFrame(dic)
print(df)
df = pandas.DataFrame(dic,index=[8,7,8])
print(df)

对应效果:

PS:如果以Dictionary作为数据源,Column自动生成且无法更改

         index修改时要用列表的形式

二、插入行

1️⃣append

从末尾加入使用的是append函数

下面是append函数的参数:

Parameters

other : DataFrame or Series/dict-like object, or list of these
    The data to append.
ignore_index : bool, default False
    If True, the resulting axis will be labeled 0, 1, …, n - 1.

verify_integrity : bool, default False
    If True, raise ValueError on creating index with duplicates.
sort : bool, default False
    Sort columns if the columns of self and other are not aligned.

Returns

DataFrame
    A new DataFrame consisting of the rows of caller and the rows of other.

See Also

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值