DataFrame的基本用法
建立数据表
import pandas as pd#加载pandas模块
d={
'name':['Harry','Ron','Hermione'],
'sex':['M','M','F'],
'age':[11,12,10]
}
df=pd.DataFrame(d,index=list('abc'))
df
name sex age
a Harry M 11
b Ron M 12
c Hermione F 10
数据表的操作
查看表的信息
#查看表的信息,.info()
df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 3 entries, a to c
Data columns (total 3 columns):
name 3 non-null object
sex 3 non-null object
age 3 non-null int64
dtypes: int64(1), object(2)
memory usage: 176.0+ bytes
索引切片
# df[[‘name’,’age’]] name age a Harry 11 b Ron 12 c Hermione 10
- 基于行的切片([点击查看loc与iloc的更多具体用法](https://blog.csdn.net/W_weiying/article/details/81411257?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.compare&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.compare))
```python
#基于行的切片,iloc按行数提取
df.iloc[2]
name Hermione
sex F
age 10
Name: c, dtype: object
##基于行的切片,loc按索引名提取
df.loc['c']
name Hermione
sex F
age 10
Name: c, dtype: object
- 修改数值
- 修改整列值 ```python df name sex age a Harry M 11 b Ron M 12 c Hermione F 10
方法1:表名.列名=修改后的值
df.age=20 df name sex age a Harry M 20 b Ron M 20 c Hermione F 20
方法2索引:表名[‘列名’]=修改后的值
df[‘age’]=21 df name sex age a Harry M 21 b Ron M 21 c Hermione F 21
- 修改某个值
```python
#方法:多重索引定位后赋值
df['age'][2]=30
df
name sex age
a Harry M 21
b Ron M 21
c Hermione F 30
- 修改索引 ```python df.index Index([‘a’, ‘b’, ‘c’], dtype=’object’)
df.index=list(‘123’) df name sex age 1 Harry M 21 2 Ron M 21 3 Hermione F 30 ```