点击查看【bilibili】

DataFrame的基本用法

建立数据表

  1. import pandas as pd#加载pandas模块
  2. d={
  3. 'name':['Harry','Ron','Hermione'],
  4. 'sex':['M','M','F'],
  5. 'age':[11,12,10]
  6. }
  7. df=pd.DataFrame(d,index=list('abc'))
  8. df
  9. name sex age
  10. a Harry M 11
  11. b Ron M 12
  12. c Hermione F 10

数据表的操作

  • 查看表的信息

    1. #查看表的信息,.info()
    2. df.info()
    3. <class 'pandas.core.frame.DataFrame'>
    4. Index: 3 entries, a to c
    5. Data columns (total 3 columns):
    6. name 3 non-null object
    7. sex 3 non-null object
    8. age 3 non-null int64
    9. dtypes: int64(1), object(2)
    10. memory usage: 176.0+ bytes
  • 索引切片

    • 基于列的切片 ```python

      基于列的切片

      df[‘age’] a 11 b 12 c 10 Name: age, dtype: int64

# 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 ```