点击上方“早起Python”,关注并星标公众号
和我一起玩Python
本文精心挑选在数据处理中常见的120种操作并整理成习题发布。并且每一题同时给出Pandas与R语言解法,同时针对部分习题给出了多种方法与注解。本系列一共涵盖了数据处理、计算、可视化等常用操作,动手敲一遍代码一定会让你有所收获!
1 创建DataFrame 题目 :将下面的字典创建为DataFrame"grammer":[
难度
:⭐
期望结果
Python解法
import numpy
R语言解法
# R中没有字典概念,故直接创建dataframe/tibble
注:1-20题均基于该数据框给出 2
数据提取
题目
:提取含有字符串"Python"
的行
难度
:⭐⭐
期望结果
grammer
Python解法
:
#> 1
R语言解法
which(df
3
提取列名
题目
:输出df的所有列名
难度
:
⭐
期望结果
Index([
Python解法
R语言解法
names(df)
# [1] "grammer" "score"
4
修改列名
题目
:修改第二列列名为'popularity'
难度
:⭐⭐
Python解法
rename(columns={
R语言解法
df %
rename(popularity = score)
5
字符统计
题目
:统计grammer列中每种编程语言出现的次数
难度
:⭐⭐
Python解法
df
R语言解法
# 神方法table
6
缺失值处理
题目
:将空值用上下值的平均值填充
难度
:
⭐⭐⭐
Python解法
# pandas里有一个插值方法,就是计算缺失值上下两数的均值
R语言解法
library(Hmisc)
index is.na(df$popularity))
df$popularity (unlist(df[index-1, 2] +
df[index+1, 2]))/2)
7
数据提取
题目
:提取popularity列中值大于3的行
难度
:⭐⭐
Python解法
[df['popularity'] > 3]
R语言解法
df %>%
filter(popularity > 3)
# 等价于
df[df$popularity > 3,] # 这种方法跟pandas很相似
8
数据去重
题目
:按照grammer列进行去重
难度
:
⭐⭐
Python解法
df
R语言解法
[!duplicated(df$grammer),]
9
数据计算
题目
:计算popularity列平均值
难度
:
⭐⭐
Python解法
'popularity'].mean()
R语言解法
#> 第一种
10
格式转换
题目
:将grammer列转换为list
难度
:
⭐⭐
Python解法
'grammer'].to_list()
R解法
unlist(df$grammer)
# [1] "Python" "C" "Java" "GO" NA "SQL" "PHP" "Python"
11
数据保存
题目
:将DataFrame保存为EXCEL
难度
:
⭐⭐
Python解法
df
R解法
#R对EXCEL文件不太友好
12
数据查看
题目
:查看数据行列数
难度
:
⭐
Python解法
df.shape
# (8, 2)
R解法
dim(df)
# [1] 8 2
13
数据提取
题目
:提取popularity列值大于3小于7的行
难度
:
⭐⭐
Python解法
df
R解法
library(dplyr)
df %>%
filter(popularity > 3 & popularity <7)
# 等价于
df[(df$popularity > 3) & (df$popularity <7),]
14
位置处理
题目
:交换两列位置
难度
:
⭐⭐⭐
Python解法
'popularity']
R解法
df %
15
数据提取
题目
:提取popularity列最大值所在行
难度
:
⭐⭐
Python解法
'popularity'] == df[
R解法
df %>%
filter(popularity == max(popularity))
# 同理也有类似pandas的方法
df[df$popularity == max(df$popularity),]
16
数据查看
题目
:查看最后5行数据
难度
:
⭐
Python解法
df.tail()
R解法
# R中head和tail默认是6行,可以指定数字
17
数据修改
题目
:删除最后一行数据
难度
:
⭐
Python解法
df = df.drop(labels=df.shape[
R解法
1],]
18
数据修改
题目
:添加一行数据['Perl',6.6]
难度
:
⭐⭐
Python解法
row = {
R解法
'Perl')
19
数据整理
题目
:对数据按照"popularity"列值的大小进行排序
难度
:
⭐⭐
Python解法
"popularity",inplace=
R解法
df %
20
字符统计
题目
:统计grammer列每个字符串的长度
难度
:
⭐⭐⭐
Python解法
'grammer'] = df[
R解法
library(Hmisc)
library(stringr)
df$grammer $grammer,'R')
str_length(df$grammer)
df$len_str $grammer)
第二期:数据处理基础
21 数据读取 题目 :读取本地EXCEL数据 难度 : ⭐ Python解法import pandas
R解法
#R语言处理excel不友好,直接读取日期时间数据会变成实数
21—50部分习题与该数据相关
22
数据查看
题目
:查看df数据前5行
难度
:
⭐
期望输出
Python解法
df
R解法
# 默认是6行,可指定行数
23
数据计算
题目
:将
salary列数据转换为最大值与最小值的平均值
难度
:
⭐⭐⭐⭐
期望输出
Python解法
# 方法一:apply + 自定义函数
R解法
library(stringr)
df$salary %
str_replace_all('k','') %>%
str_split('-',simplify = T) %>%
apply(2,as.numeric) %>%
rowMeans() * 1000
24
数据分组
题目
:将数据根据学历进行分组并计算平均薪资
难度
:
⭐⭐⭐
期望输出
education
Python解法
df
R解法
df %>%
25
时间转换
题目
:将
createTime
列时间转换为
月-日
难度
:
⭐⭐⭐
期望输出
Python解法
for
R解法
#转化后该列属性是 字符串,R中对时间格式要求严格
26
数据查看
题目
:查看索引、数据类型和内存信息
难度
:
⭐
期望输出
class 'pandas.core.frame.DataFrame'>
Python解法
df
R解法
str(df)
# 内存查看需要用到其他的库
library(pryr)
object_size(df)
# 6.66 kB
27
数据查看
题目
:查看数值型列的汇总统计
难度
:
⭐
Python解法
df
R解法
summary(df)
28
数据整理
题目
:新增一列根据salary将数据分为三组
难度
:
⭐⭐⭐⭐
输入
期望输出
Python解法
bins = [
R解法
#用ifelse也可以
29
数据整理
题目
:按照salary列对数据降序排列
难度
:
⭐⭐
Python解法
'salary', ascending=
R解法
df %>%
3
0
数据提取
题目
:取出第33行数据
难度
:
⭐⭐
Python解法
df
R解法
[33,]
31
数据计算
题目
:计算
salary列的中位数
难度
:
⭐⭐
Python解法
'salary'])
R解法
median(df$salary)
# [1] 17500
32
数据可视化
题目
:绘制薪资水平频率分布直方图
难度
:
⭐⭐⭐
期望输出
Python解法
# Jupyter运行matplotlib成像需要运行魔术命令
R解法
library(ggplot2)
33
数据可视化
题目
:绘制薪资水平密度曲线
难度
:
⭐⭐⭐
期望输出
Python解法
df
R解法
df %>%
34
数据删除
题目
:删除最后一列categories
难度
:
⭐
Python解法
del df[
R解法
df 4]
35
数据处理
题目
:将df的第一列与第二列合并为新的一列
难度
:
⭐⭐
Python解法
'test'] = df[
R解法
df %
mutate(test = paste0(df$education,df$createTime))
36
数据处理
题目
:
将education列
与salary列合并为新的一列
难度
:
⭐⭐⭐
备注:
sala
ry为int类型,操作与35题有所不同
Python解法
"test1"] = df[
R解法
df %
37
数据计算
题目
:计算salary最大值与最小值之差
难度
:
⭐⭐⭐
Python解法
'salary']].apply(
R解法
df %>%
summarise(delta = max(salary) - min(salary)) %>%
unlist()
# delta
# 41500
38
数据处理
题目
:将第一行与最后一行拼接
难度
:
⭐⭐
Python解法
pd
R解法
[1,],df
39
数据处理
题目
:将第8行数据添加至末尾
难度
:
⭐⭐
Python解法
df
R解法
[8,])
40
数据查看
题目
:查看每列的数据类型
难度
:
⭐
期望结果
object
Python解法
df.dtypes
# createTime object
# education object
# salary int64
# test object
# test1 object
# dtype: object
R解法
str(df)
# tibble [135 x 5] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
# $ createTime: chr [1:135] "03-16" "03-16" "03-16" "03-16" ...
# $ education : chr [1:135] "本科" "本科" "不限" "本科" ...
# $ salary : num [1:135] 27500 30000 27500 16500 15000 14000 23000 12500 7000 16000 ...
# $ test : chr [1:135] "本科03-16" "本科03-16" "不限03-16" "本科03-16" ...
# $ test1 : chr [1:135] "27500本科" "30000本科" "27500不限" "16500本科" ...
41
数据处理
题目
:将createTime列设置为索引
难度
:
⭐⭐
Python解法
df
R解法
df %>%
tibble::column_to_rownames('createTime')
42
数据创建
题目
:生成一个和df长度相同的随机数dataframe
难度
:
⭐⭐
Python解法
df1 = pd.DataFrame(pd.Series(np.random.randint(
R解法
df1 135,function(n) {
replicate(n,sample(1:10,1))
})
# 列名暂时不一样,下一题重命名
43
数据处理
题目
:将上一题生成的dataframe与df合并
难度
:
⭐⭐
Python解法
df= pd.concat([df,df1],axis=
R解法
df %
rename(`0` = df1)
# 非常规命名需要用``包裹变量名
44
数据计算
题目
:生成新的一列
new
为
salary
列减去之前生成随机数列
难度
:
⭐⭐
Python解法
"new"] = df[
R解法
df %
mutate(new = salary - `0`)
45
缺失值处理
题目
:检查数据中是否含有任何缺失值
难度
:
⭐⭐⭐
Python解法
df.isnull().values.any()
# False
R解法
# 这个包的结果呈现非常有趣
46
数据转换
题目
:将
salary
列类型转换为浮点数
难度
:
⭐⭐⭐
Python解法
df
R解法
as.double(df2$salary)
47
数据计算
题目
:计算
salary
大于10000的次数
难度
:
⭐⭐
Python解法
'salary'] >
R解法
df %>%
48
数据统计
题目
:查看每种学历出现的次数
难度
:
⭐⭐⭐
期望输出
119
Python解法
df
R解法
table(df$education)
49
数据查看
题目
:查看
education
列共有几种学历
难度
:
⭐⭐
Python解法
'education'].nunique()
R解法
length(unique(df$education))
# [1] 4
50
数据提取
题目
:提取
salary
与
new
列的和大于60000的最后3行
难度
:
⭐⭐⭐⭐
期望输出
Python解法
rowsums = df[['salary','new']].apply(np.sum, axis=1)
res = df.iloc[np.where(rowsums > 60000)[0][-3:], :]
R解法
df[df$salary + df
51
数据读取
题目
:使用绝对路径读取本地Excel数据
难度
:
⭐
Python解法
import pandas
R解法
# 转存csv后再读
备注
请将答案中路径替换为自己机器存储数据的绝对路径,51—80相关习题与该数据有关
52 数据查看 题目 :查看数据前三行 难度 : ⭐ 期望结果 Python解法df
R解法
head(df,
53
缺失值处理
题目
:查看每列数据缺失值情况
难度
:
⭐
⭐
期望结果
1
Python解法
df
R解法
is.na(df))
54
缺失值处理
题目
:提取日期列含有空值的行
难度
:
⭐
⭐
期望结果
Python解法
df
R解法
[is.na(df$日期),]
55
缺失值处理
题目
:输出每列缺失值具体行数
难度
:
⭐
⭐⭐
期望结果
[327]行位置有缺失值
Python解法
for i in df.column
R解法
library(glue)
for (i in names(df)){
if(sum(is.na(df[,'日期'])) != 0){
res1 is.na(df[,i]))
res2 ',')
print(glue('列名:"{i}", 第[{res2}]行有缺失值'))
}
}
56
缺失值处理
题目
:删除所有存在缺失值的行
难度
:
⭐
⭐
Python解法
0, how=
R解法
df <
备注
0-行操作(默认),
57
数据可视化
题目
:绘制收盘价的折线图
难度
:
⭐
⭐
期望结果
Python解法
# Jupyter运行matplotlib
R解法
library(ggplot2)
58
数据可视化
题目
:同时绘制开盘价与收盘价
难度
:
⭐
⭐⭐
期望结果
Python解法
'font.sans-serif'] = [
R解法
df %>%
ggplot() +
geom_line(aes(日期,`收盘价(元)`), size=1.2, color='steelblue') +
geom_line(aes(日期,`开盘价(元)`), size=1.2, color='orange') +
ylab(c('价格(元)'))
# 这种画出来没有图例,当然可以手动添加,但为了映射方便可以用另一种方法
library(tidyr)
df %>%
select(日期,`开盘价(元)`,`收盘价(元)`) %>%
pivot_longer(c(`开盘价(元)`,`收盘价(元)`),
names_to='type',values_to='price') %>%
ggplot(aes(日期,price,color=type)) +
geom_line(size=1.2) +
scale_color_manual(values=c('steelblue','orange')) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.title = element_blank(),
legend.position = c(0.86, 0.9)
)
59
数据可视化
题目
:绘制涨跌幅的直方图
难度
:
⭐
⭐
期望结果
Python解法
'涨跌幅(%)'])
R解法
df %>%
60
数据可视化
题目
:让直方图更细致
难度
:
⭐
⭐
期望结果
Python解法
df
R解法
df %>%
61
数据创建
题目:
以data的列名创建一个dataframe
难度
:
⭐
⭐
Python解法
temp = pd.DataFrame(columns = df.columns.to_list())
R解法
temp
62
异常值处理
题目:
打印所有换手率不是数字的行
难度
:
⭐
⭐⭐
期望结果
Python解法
for index,row
R解法
#换手率这一列属性为chr,需要先强转数值型
63
异常值处理
题目:
打印所有换手率为--的行
难度
:
⭐
⭐⭐
Python解法
'换手率(%)'] ==
R解法
df %>%
备注
通过上一题我们发现换手率的异常值只有--
64 数据处理 题目: 重置data的行号 难度 : ⭐ Python解法df = df.reset_index(drop=
R解法
rownames(df)
# 如果是tibble则索引始终是按顺序
备注
有时我们修改数据会导致索引混乱
65 异常值处理 题目: 删除所有换手率为非数字的行 难度 : ⭐ ⭐⭐ Python解法lst = []
for index,row in df.iterrows():
if type(row[13]) != float:
lst.append(index)
df.drop(labels=lst,inplace=True)
R解法
is.na(
66
数据可视化
题目:
绘制
换手率的密度曲线
难度
:
⭐
⭐⭐
期望结果
Python解法
df
R解法
df$`换手率(%)` as.double(df$`换手率(%)`)
ggplot(df) +
geom_density(aes(`换手率(%)`))
67
数据计算
题目:
计算前一天与后一天收盘价的差值
难度
:
⭐
⭐
Python解法
df
R解法
df %>%
68
数据计算
题目:
计算前一天与后一天收盘价变化率
难度
:
⭐
⭐
Python解法
data
R解法
df %>%
69
数据处理
题目:
设置日期为索引
难度
:
⭐
Python解法
df
R解法
df %>%
column_to_rownames(var='日期')
70
指标计算
题目:以5个数据作为一个数据滑动窗口,在这个5个数据上取均值(收盘价)
难度 : ⭐ ⭐ ⭐ Python解法df
R解法
library(RcppRoll)
71
指标计算
题目:以5个数据作为一个数据滑动窗口,计算这五个数据总和(收盘价)
难度 : ⭐ ⭐ ⭐ Python解法df
R解法
df %>%
72
数据可视化
题目:将收盘价5日均线、20日均线与原始数据绘制在同一个图上
难度 : ⭐ ⭐ ⭐ 期望结果 Python解法df
R解法
df %>%
73
数据重采样
题目:按周为采样规则,取一周收盘价最大值
难度 : ⭐ ⭐ ⭐ Python解法'日期')
R解法
library(plyr)
res "1 week")),"[")
res_max res,function(n)max(n$`收盘价(元)`),simplify=TRUE)
as.data.frame(res_max)
74
数据可视化
题目:绘制重采样数据与原始数据
难度 : ⭐ ⭐ ⭐ 期望结果 Python解法df
R解法
res %>%
75
数据处理
题目
:将数据往后移动5天
难度
:
⭐
⭐
Python解法
df
R解法
lag(df,
76
数据处理
题目
:将数据向前移动5天
难度
:
⭐
⭐
Python解法
df
R解法
lead(df,
77
数据计算
题目
:使用expending函数计算开盘价的移动窗口均值
难度
:
⭐
⭐
Python解法
df
R解法
#R中没有expanding完全一致的函数
78
数据可视化
题目
:绘制上一题的移动均值与原始数据折线图
难度
:
⭐
⭐⭐
期望结果
Python解法
'expanding Open mean']=df[
R解法
library(tidyr)
df %>%
cbind(res) %>%
dplyr::rename(Opening_Price = `开盘价(元)`,
Expanding_Open_Mean = cummean) %>%
select(日期,Opening_Price,Expanding_Open_Mean) %>%
pivot_longer(c(Opening_Price,Expanding_Open_Mean),
names_to = 'type',
values_to ='price') %>%
ggplot(aes(日期,price,color = type)) +
geom_line(size=1.2) +
scale_color_manual(values=c('orange','steelblue')) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.title = element_blank(),
legend.position = c(0.9, 0.9)
)
79
数据计算
题目
:计算布林指标
难度
:
⭐
⭐⭐⭐
Python解法
'former 30 days rolling Close mean']=df[
R解法
df %
mutate(avg_20 = roll_mean(`收盘价(元)`,n = 20,align="right",fill = NA),
upper_bound = avg_20 + 2 * roll_sd(`收盘价(元)`,n = 20,align="right",fill = NA),
lower_bound = avg_20 - 2 * roll_sd(`收盘价(元)`,n = 20,align="right",fill = NA))
80
数据可视化
题目
:计算布林线并绘制
难度
:
⭐
⭐⭐
期望结果
Python解法
'收盘价(元)',
R解法
df %>%
dplyr::rename(former_30_days_rolling_Close_mean = avg_20,
Closing_Price = `收盘价(元)`) %>%
select(日期,Closing_Price,
former_30_days_rolling_Close_mean,upper_bound,lower_bound) %>%
pivot_longer(c(Closing_Price,former_30_days_rolling_Close_mean,upper_bound,lower_bound),
names_to = 'type',
values_to ='price') %>%
ggplot(aes(日期,price,color = type)) +
geom_line(size=1.2) +
scale_color_manual(values=c('steelblue','orange','red','green')) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.title = element_blank(),
legend.position = c(0.6, 0.2)
)
81
数据查看
题目
:导入并查看pandas与numpy版本
难度
:
⭐
Python解法
import pandas
R语言解法
"tidyverse")
82
数据创建
题目
:从NumPy数组创建DataFrame
难度
:
⭐
备注
使用numpy生成20个0-100随机数
Python解法
tem = np.random.randint(
R语言解法
function(n) {
83
数据创建
题目
:从NumPy数组创建DataFrame
难度
:
⭐
备注
使用numpy生成20个0-100固定步长的数
Python解法
tem = np.arange(
R语言解法
0,
84
数据创建
题目
:从NumPy数组创建DataFrame
难度
:
⭐
备注
使用numpy生成20个指定分布(如标准正态分布)的数
Python解法
tem = np.random.normal(
R语言解法
20,
85
数据创建
题目
:将df1,df2,df3按照行合并为新DataFrame
难度
:
⭐⭐
Python解法
df = pd.concat([df1,df2,df3],axis=
R语言解法
df
86
数据创建
题目
:将df1,df2,df3按照列合并为新DataFrame
难度
:
⭐⭐
期望结果
0 1 2
0 95 0 0.022492
1 22 5 -1.209494
2 3 10 0.876127
3 21 15 -0.162149
4 51 20 -0.815424
5 30 25 -0.303792
...............
Python解法
df = pd.concat([df1,df2,df3],axis=
R语言解法
df names(df) 0,
87
数据查看
题目
:查看df所有数据的最小值、25%分位数、中位数、75%分位数、最大值
难度
:
⭐⭐
Python解法
np
R语言解法
summary(unlist(df))
88
数据修改
题目
:修改列名为col1,col2,col3
难度
:
⭐
Python解法
'col1',
R语言解法
df %
dplyr::rename(col1 = 1,
col2 = 2,
col3 = 3)
# 或者用类似pandas的方法
names(df) 'col1','col2','col3')
89
数据提取
题目
:提取第一列中不在第二列出现的数字
难度
:
⭐⭐⭐
Python解法
'col1'][
R语言解法
$col1 %
90
数据提取
题目
:提取第一列和第二列出现频率最高的三个数字
难度
:
⭐⭐⭐
Python解法
'col1'].
R语言解法
count(unlist(c(df$col1,df
91
数据提取
题目
:提取第一列中可以整除5的数字位置
难度
:
⭐⭐⭐
Python解法
np
R语言解法
which(df[
92
数据计算
题目
:计算第一列数字前一个与后一个的差值
难度
:
⭐⭐
Python解法
df
R语言解法
df %>%
93
数据处理
题目
:将col1,col2,clo3三列顺序颠倒
难度
:
⭐⭐
Python解法
df
R语言解法
df %>%
94
数据提取
题目
:提取第一列位置在1,10,15的数字
难度
:
⭐⭐
Python解法
'col1'].take([
R语言解法
[c(1,10,15) + 1,1]
95
数据查找
题目
:查找第一列的局部最大值位置
难度
:
⭐⭐⭐⭐
备注
即比它前一个与后一个数字的都大的数字
Python解法
res = np.diff(np.
R语言解法
0))
96
数据计算
题目
:按行计算df的每一行均值
难度
:
⭐⭐
Python解法
'col1',
R语言解法
rowMeans(df)
97
数据计算
题目
:对第二列计算移动平均值
难度
:
⭐⭐⭐
备注
每次移动三个位置,不可以使用自定义函数
Python解法
'col2'], np.ones(
R语言解法
library(RcppRoll)
98
数据修改
题目
:将数据按照第三列值的大小升序排列
难度
:
⭐⭐
Python解法
"col3",inplace=
R语言解法
df %
arrange(col3)
99
数据修改
题目
:将第一列大于50的数字修改为'高'
难度
:
⭐⭐
Python解法
'col1'] >
R语言解法
df[df$col1 > 50,1] '高'
100
数据计算
题目
:计算第一列与第二列之间的欧式距离
难度
:
⭐⭐⭐
备注
不可以使用自定义函数
Python解法
'col1']-df[
R语言解法
# 可以利用概念计算
101
数据读取
题目
:从CSV文件中读取指定数据
难度
:
⭐⭐
备注
从数据1中的前10行中读取positionName, salary两列
Python解法
r'C:\Users\chenx\Documents\Data Analysis\数据1.csv',encoding=
R语言解法
#一步读取文件的指定列用readr包或者原生函数都没办法
102
数据读取
题目
:从CSV文件中读取指定数据
难度
:
⭐⭐
备注
从数据2中读取数据并在读取数据时将薪资大于10000的为改为高
Python解法
r'C:\Users\chenx\Documents\Data Analysis\数据2.csv',
R语言解法
library(readr)
df2 '数据2.csv') %>%
mutate('学历要求',
'薪资水平' = ifelse(
薪资水平 > 10000,'高','低'))
103
数据计算
题目
:从dataframe提取数据
难度
:
⭐⭐⭐
备注
从上一题数据中,对薪资水平列每隔20行进行一次抽样
期望结果
Python解法
::20, :][
R语言解法
[seq(1,dim(df2)[1],20),]
104
数据处理
题目
:将数据取消使用科学计数法
难度
:
⭐⭐
输入
df = pd.DataFrame(np.random.random(
期望结果
Python解法
10)**
R语言解法
10)
105
数据处理
题目
:将上一题的数据转换为百分数
难度
:
⭐⭐⭐
期望结果
Python解法
'data':
R语言解法
tibble(data = str_glue(
106
数据查找
题目
:查找上一题数据中第3大值的行号
难度
:
⭐⭐⭐
Python解法
df
R语言解法
df %>%
107
数据处理
题目
:反转df的行
难度
:
⭐⭐
Python解法
df
R语言解法
df %>%
108
数据重塑
题目
:按照多列对数据进行合并
难度
:
⭐⭐
输
入
'key1': [
Python解法
on=[
R语言解法
"key1" = c(
109
数据重塑
题目
:按照多列对数据进行合并
难度
:
⭐⭐
备注
只保存df1的数据
Python解法'left',
R语言解法
by = c(
110
数据处理
题目
:再次读取数据1并显示所有的列
难度
:
⭐⭐
备注
数据中由于列数较多中间列不显示
Python解法
r'C:\Users\chenx\Documents\Data Analysis\数据1.csv',encoding=
R语言解法
"GBK")) %>%
111
数据查找
题目
:查找secondType与thirdType值相等的行号
难度
:
⭐⭐
Python解法
where(df.secondType == df.thirdType)
R语言解法
df %>%
112
数据查找
题目
:查找薪资大于平均薪资的第三个数据
难度
:
⭐⭐⭐
Python解法
'salary'] > df[
R语言解法
df %>%
mutate(nrow = rownames(.)) %>%
filter(salary > mean(salary)) %>%
select(nrow) %>%
filter(row_number() == 3)
# # A tibble: 1 x 1
# nrow
#
# 1 6
113
数据计算
题目
:将上一题数据的salary列开根号
难度
:
⭐⭐
Python解法
df
R语言解法
df %>%
summarise(salary_sqrt = sqrt(salary))
114
数据处理
题目
:将上一题数据的linestaion列按_拆分
难度
:
⭐⭐
Python解法
'split'] = df[
R语言解法
df %
mutate(split = str_split(linestaion,'_'))
115
数据查看
题目
:查看上一题数据中一共有多少列
难度
:
⭐
Python解法
1]
R语言解法
length(df)
# [1] 54
116
数据提取
题目
:提取industryField列以'数据'开头的行
难度
:
⭐⭐
Python解法
df
R语言解法
grep(
117
数据计算
题目
:以salary score 和 positionID制作数据透视
难度
:
⭐⭐⭐
Python解法
values=[
R语言解法
df %
group_by(positionId) %>%
dplyr::summarise(salary = mean(salary),
score = mean(score)) %>%
as.data.frame(.)
rownames(df) NULL
tibble::column_to_rownames(df,var='positionId')
118
数据计算
题目
:同时对salary、score两列进行计算
难度
:
⭐⭐⭐
Python解法
"salary",
R语言解法
res %
119
数据计算
题目
:对不同列执行不同的计算
难度
:
⭐⭐⭐
备注
对salary求平均,对score列求和
Python解法
"salary"
R语言解法
df %>%
120
数据计算
题目
:计算并提取平均薪资最高的区
难度
:
⭐⭐⭐⭐
Python解法
df
R语言解法
df %>%
以上就是玩转数据处理120题全部内容,如果能坚持走到这里的读者,我想你已经掌握了处理数据的常用操作,并且在之后的数据分析中碰到相关问题,希望武装了Pandas的你能够从容的解决!
另外我已将习题与源码整理成电子版,后台回复pandas即可下载,我们下个专题见,拜拜~