## 导入相关库
import pandas as pd
import numpy as np
# 1.将下面的字典创建为DataFrame
data = {"grammer":["Python","C","Java","GO",np.nan,"SQL","PHP","Python"],
"score":[1,2,np.nan,4,5,6,7,10]}
df = pd.DataFrame(data)
df
| grammer | score |
|---|
| 0 | Python | 1.0 |
|---|
| 1 | C | 2.0 |
|---|
| 2 | Java | NaN |
|---|
| 3 | GO | 4.0 |
|---|
| 4 | NaN | 5.0 |
|---|
| 5 | SQL | 6.0 |
|---|
| 6 | PHP | 7.0 |
|---|
| 7 | Python | 10.0 |
|---|
# 2.提取含有字符串"Python"的行
# 第一种方法
df[df['grammer'] == 'Python']
# 第二种方法
results = df['grammer'].str.contains("Python")
results.fillna(value=False,inplace = True)
df[results]
| grammer | score |
|---|
| 0 | Python | 1.0 |
|---|
| 7 | Python | 10.0 |
|---|
# 3.输出df的所有列名
df.columns
Index(['grammer', 'score'], dtype='object')
# 4.修改第二列列名为'popularity'
df.rename(columns={'score':'popularity'},inplace = True)
df
| grammer | popularity |
|---|
| 0 | Python | 1.0 |
|---|
| 1 | C | 2.0 |
|---|
| 2 | Java | NaN |
|---|
| 3 | GO | 4.0 |
|---|
| 4 | NaN | 5.0 |
|---|
| 5 | SQL | 6.0 |
|---|
| 6 | PHP | 7.0 |
|---|
| 7 | Python | 10.0 |
|---|
# 5.统计grammer列中每种编程语言出现的次数
df['grammer'].value_counts()
Python 2
SQL 1
Java 1
PHP 1
GO 1
C 1
Name: grammer, dtype: int64
# 6.将空值用上下值的平均值填充
df['popularity'] = df['popularity'].fillna(df['popularity'].interpolate())
df
| grammer | popularity |
|---|
| 0 | Python | 1.0 |
|---|
| 1 | C | 2.0 |
|---|
| 2 | Java | 3.0 |
|---|
| 3 | GO | 4.0 |
|---|
| 4 | NaN | 5.0 |
|---|
| 5 | SQL | 6.0 |
|---|
| 6 | PHP | 7.0 |
|---|
| 7 | Python | 10.0 |
|---|
# 7.提取popularity列中值大于3的行
df[df['popularity']>3]
| grammer | popularity |
|---|
| 3 | GO | 4.0 |
|---|
| 4 | NaN | 5.0 |
|---|
| 5 | SQL | 6.0 |
|---|
| 6 | PHP | 7.0 |
|---|
| 7 | Python | 10.0 |
|---|
# 8.按照grammer列进行去除重复值
df.drop_duplicates(['grammer'])
| grammer | popularity |
|---|
| 0 | Python | 1.0 |
|---|
| 1 | C | 2.0 |
|---|
| 2 | Java | 3.0 |
|---|
| 3 | GO | 4.0 |
|---|
| 4 | NaN | 5.0 |
|---|
| 5 | SQL | 6.0 |
|---|
| 6 | PHP | 7.0 |
|---|
# 9.计算popularity列平均值
df['popularity'].mean()
4.75
# 10.将grammer列转换为list
df['grammer'].to_list()
['Python', 'C', 'Java', 'GO', nan, 'SQL', 'PHP', 'Python']
# 11.将DataFrame保存为EXCEL
df.to_excel('test.xlsx')
# 12.查看数据行列数
df.shape
(8, 2)
# 13.提取popularity列值大于3小于7的行
df[(df['popularity']>3) & (df['popularity']<7)]
| grammer | popularity |
|---|
| 3 | GO | 4.0 |
|---|
| 4 | NaN | 5.0 |
|---|
| 5 | SQL | 6.0 |
|---|
# 14.交换两列位置
temp = df['popularity']
df.drop(labels=['popularity'], axis=1,inplace = True)
# insert中第一个参数是列的位置,第二参数是名称,第三个对象
df.insert(0, 'popularity', temp)
df
| popularity | grammer |
|---|
| 0 | 1.0 | Python |
|---|
| 1 | 2.0 | C |
|---|
| 2 | 3.0 | Java |
|---|
| 3 | 4.0 | GO |
|---|
| 4 | 5.0 | NaN |
|---|
| 5 | 6.0 | SQL |
|---|
| 6 | 7.0 | PHP |
|---|
| 7 | 10.0 | Python |
|---|
# 15.提取popularity列最大值所在行
df[df['popularity'] == df['popularity'].max()]
| popularity | grammer |
|---|
| 7 | 10.0 | Python |
|---|
# 16.查看最后5行数据
df.tail()
| popularity | grammer |
|---|
| 3 | 4.0 | GO |
|---|
| 4 | 5.0 | NaN |
|---|
| 5 | 6.0 | SQL |
|---|
| 6 | 7.0 | PHP |
|---|
| 7 | 10.0 | Python |
|---|
# 17.删除最后一行数据
# 减1是因为是从第0行开始记数的
df.drop(len(df)-1,inplace = True)
df
| popularity | grammer |
|---|
| 0 | 1.0 | Python |
|---|
| 1 | 2.0 | C |
|---|
| 2 | 3.0 | Java |
|---|
| 3 | 4.0 | GO |
|---|
| 4 | 5.0 | NaN |
|---|
| 5 | 6.0 | SQL |
|---|
| 6 | 7.0 | PHP |
|---|
# 18.添加一行数据['Perl',6.6]
row={'grammer':'Perl','popularity':6.6}
df = df.append(row,ignore_index=True)
df
| popularity | grammer |
|---|
| 0 | 1.0 | Python |
|---|
| 1 | 2.0 | C |
|---|
| 2 | 3.0 | Java |
|---|
| 3 | 4.0 | GO |
|---|
| 4 | 5.0 | NaN |
|---|
| 5 | 6.0 | SQL |
|---|
| 6 | 7.0 | PHP |
|---|
| 7 | 6.6 | Perl |
|---|
# 19.对数据按照"popularity"列值的大小进行排序
df.sort_values('popularity',inplace = True)
df
| popularity | grammer |
|---|
| 0 | 1.0 | Python |
|---|
| 1 | 2.0 | C |
|---|
| 2 | 3.0 | Java |
|---|
| 3 | 4.0 | GO |
|---|
| 4 | 5.0 | NaN |
|---|
| 5 | 6.0 | SQL |
|---|
| 7 | 6.6 | Perl |
|---|
| 6 | 7.0 | PHP |
|---|
# 20.统计grammer列每个字符串的长度
df['grammer'] = df['grammer'].fillna('R')
df['len_str'] = df['grammer'].map(lambda x: len(x))
df.loc[df[df['grammer']=='R'].index,'len_str'] = '未知'
df.loc[df[df['grammer']=='R'].index,'grammer'] = '未知'
df
| popularity | grammer | len_str |
|---|
| 0 | 1.0 | Python | 6 |
|---|
| 1 | 2.0 | C | 1 |
|---|
| 2 | 3.0 | Java | 4 |
|---|
| 3 | 4.0 | GO | 2 |
|---|
| 4 | 5.0 | 未知 | 未知 |
|---|
| 5 | 6.0 | SQL | 3 |
|---|
| 7 | 6.6 | Perl | 4 |
|---|
| 6 | 7.0 | PHP | 3 |
|---|