列表和数据框的练习

> x1<-list(seq(from=-4,to=40,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),dim=c(3,3)),"'don'' ''qulxote",factor(x=c("LOW","MED","MED",HIGH")))
+ x1

+ l
+ l
+ x1<-list(seq(from=-4,to=40,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),dim=c(3,3)),"'don'' ''qulxote",factor(x=c("LOW","MED","MED",HIGH")))
错误: unexpected string constant in:
"l
x1<-list(seq(from=-4,to=40,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),dim=c(3,3)),""
> x1<-list(seq(from=-4,to=4,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),dim=c(3,3)),"'don'' ''qulxote",factor(x=c("LOW","MED","MED",HIGH")))
+ x1
+ x2<-seq(from=-4,to=4,length=20)
+ x2
+ fffef\
+ ejeweuiodweoieh
+ 2*6
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+ q()
+  x1<-list(seq(from=-4,to=40,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),dim=c(3,3)),"'don'' ''qulxote",factor(x=c("LOW","MED","MED",HIGH")))
错误: unexpected string constant in:
"q()
 x1<-list(seq(from=-4,to=40,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),dim=c(3,3)),""
>  x1<-list(seq(from=-4,to=40,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),nrow=3,ncol=3),"'don'' ''qulxote",factor(x=c("LOW","MED","MED",HIGH")))
+  x1<-list(seq(from=-4,to=40,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),dim=c(3,3)),"'don'' ''qulxote",factor(x=c("LOW","MED","MED",HIGH")))
错误: unexpected string constant in:
" x1<-list(seq(from=-4,to=40,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),nrow=3,ncol=3),"'don'' ''qulxote",factor(x=c("LOW","MED","MED",HIGH")))
 x1<-list(seq(from=-4,to=40,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),dim=c(3,3)),""
>  x1<-list(seq(from=-4,to=4,length=20),matrix(data=c(F,T,T,T,F,T,T,F,F),nrow=3,ncol=3),"donqulxote",factor(x=c("LOW","MED","MED","HIGH")))
> x1
[[1]]
 [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368 -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789  1.0526316  1.4736842  1.8947368  2.3157895
[17]  2.7368421  3.1578947  3.5789474  4.0000000

[[2]]
      [,1]  [,2]  [,3]
[1,] FALSE  TRUE  TRUE
[2,]  TRUE FALSE FALSE
[3,]  TRUE  TRUE FALSE

[[3]]
[1] "donqulxote"

[[4]]
[1] LOW  MED  MED  HIGH
Levels: HIGH LOW MED

> x1[[2]][c(2,1),c(2,3)]
      [,1]  [,2]
[1,] FALSE FALSE
[2,]  TRUE  TRUE
> sub(pattern="Qulxote",replacement="qulxote",x=x1[[3]])
[1] "donqulxote"
> sub(pattern="Qulxote",replacement="qulxote",x=x1[3])
[1] "donqulxote"
> sub(pattern="Qulxote",replacement="qulxote",x=x1)
[1] "c(-4, -3.57894736842105, -3.15789473684211, -2.73684210526316, -2.31578947368421, -1.89473684210526, -1.47368421052632, -1.05263157894737, -0.631578947368421, -0.210526315789474, 0.210526315789473, 0.631578947368421, 1.05263157894737, 1.47368421052632, 1.89473684210526, 2.31578947368421, 2.73684210526316, 3.1578947368421, 3.57894736842105, 4)"
[2] "c(FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE)"                                                                                                                                                                                                                                                                                             
[3] "donqulxote"                                                                                                                                                                                                                                                                                                                                              
[4] "c(2, 3, 3, 1)"                                                                                                                                                                                                                                                                                                                                           
> sub(pattern="Qulxote",replacement="qulxote",x=x1[[3]])
[1] "donqulxote"
> sub(pattern="Qulxote",replacement="qulxote",x=(x1[[3]]))
[1] "donqulxote"
> install(sub)
Error in install(sub) : 没有"install"这个函数
> install.packages(sub)
将程序包安装入‘C:/Users/dell/Documents/R/win-library/3.6’
(因为‘lib’没有被指定)
Error in endsWith(pkgs, ".zip") : non-character object(s)
> install.packages("sub")
将程序包安装入‘C:/Users/dell/Documents/R/win-library/3.6’
(因为‘lib’没有被指定)
--- 在此連線階段时请选用CRAN的鏡子 ---
Warning message:
package ‘sub’ is not available (for R version 3.6.2) 
> x1<-list(seq(from=-4,to=4,length=20),matrix(c(F,T,T,T,F,T,T,F,F),nrow=3,ncol=3),c("don","quixote"),factor(x=c("LOW","MED","LOW","MED","MED","HIGH")))
> x1
[[1]]
 [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368 -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789  1.0526316  1.4736842  1.8947368  2.3157895
[17]  2.7368421  3.1578947  3.5789474  4.0000000

[[2]]
      [,1]  [,2]  [,3]
[1,] FALSE  TRUE  TRUE
[2,]  TRUE FALSE FALSE
[3,]  TRUE  TRUE FALSE

[[3]]
[1] "don"     "quixote"

[[4]]
[1] LOW  MED  LOW  MED  MED  HIGH
Levels: HIGH LOW MED

> sub(pattern="Qulxote",replacement="qulxote",x=(x1[[3]]))
[1] "don"     "quixote"
> x1
[[1]]
 [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
 [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
[13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
[19]  3.5789474  4.0000000

[[2]]
      [,1]  [,2]  [,3]
[1,] FALSE  TRUE  TRUE
[2,]  TRUE FALSE FALSE
[3,]  TRUE  TRUE FALSE

[[3]]
[1] "don"     "quixote"

[[4]]
[1] LOW  MED  LOW  MED  MED  HIGH
Levels: HIGH LOW MED

> sub(pattern="Quixote",replacement="quixote",x=(x1[[3]]))
[1] "don"     "quixote"
> sub(pattern="d",replacement="D",x=foo[[3]][1])
Error in foo[[3]] : 下标出界
> sub(pattern="d",replacement="D",x=x1[[3]][1])
[1] "Don"
> sub(pattern="q",replacement="Q",x=x1[[3]][2])
[1] "Quixote"
> sub(pattern="Quixote",replacement="quixote",x=(x1[[3]][2]))
[1] "quixote"
> sub(pattern="Q",replacement="q",x=(x1[[3]][2]))
[1] "quixote"
> sub(pattern="quixote",replacement="Quixote",x=(x1[[3]]))
[1] "don"     "Quixote"
> x1<-sub(pattern="d",replacement="D",x=x1[[3]][1])
> x1
[1] "Don"
>  x1<-list(seq(from=-4,to=4,length=20),matrix(c(F,T,T,T,F,T,T,F,F),nrow=3,ncol=3),c("don","quixote"),factor(x=c("LOW","MED","LOW","MED","MED","HIGH")))
> x1[[3]][1]<-sub(pattern="d",replacement="D",x=x1[[3]][1])
> x1
[[1]]
 [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
 [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
[13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
[19]  3.5789474  4.0000000

[[2]]
      [,1]  [,2]  [,3]
[1,] FALSE  TRUE  TRUE
[2,]  TRUE FALSE FALSE
[3,]  TRUE  TRUE FALSE

[[3]]
[1] "Don"     "quixote"

[[4]]
[1] LOW  MED  LOW  MED  MED  HIGH
Levels: HIGH LOW MED

> x1[[3]][2]<-sub(pattern="q",replacement="Q",x=x1[[3]][2])
> x1
[[1]]
 [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
 [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
[13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
[19]  3.5789474  4.0000000

[[2]]
      [,1]  [,2]  [,3]
[1,] FALSE  TRUE  TRUE
[2,]  TRUE FALSE FALSE
[3,]  TRUE  TRUE FALSE

[[3]]
[1] "Don"     "Quixote"

[[4]]
[1] LOW  MED  LOW  MED  MED  HIGH
Levels: HIGH LOW MED

> cat("\"Windmills! ATTACK!\"\n\t-\\",foo[[3]][1]," ",foo[[3]][2],"/-",sep="")
Error in foo[[3]] : 下标出界
> cat("\"Windmills! ATTACK!\"\n\t-\\",x1[[3]][1]," ",x1[[3]][2],"/-",sep="")
"Windmills! ATTACK!"
        -\Don Quixote/-> 
> bar <- list(facs=foo[[4]],nums=c(3,2.1,3.3,4,1.5,4.9),oldlist=foo[1:3])
Error in foo[[4]] : 下标出界
> bar <- list(facs=x1[[4]],nums=c(3,2.1,3.3,4,1.5,4.9),oldlist=x1[1:3])
> [bar$nums>=3]
错误: 意外的'[' in "["
> bar$nums>=3
[1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE
> bar$facs[TRUE FALSE  TRUE  TRUE FALSE  TRUE]
错误: unexpected numeric constant in "bar$facs[TRUE FALSE"
> bar$facs[bar$nums>=3]
[1] LOW  LOW  MED  HIGH
Levels: HIGH LOW MED
> x1[[1]]>1
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
> c(x1[[1]]>1)
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
> x2<-(x[[1]]>1)
> x2
[1] TRUE
> x2<-(x[[1]][1:20]>1)
> x2
 [1] TRUE   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA
[15]   NA   NA   NA   NA   NA   NA
> x2<-(x[[1]][which(x[[1]]>1)))
错误: 意外的')' in "x2<-(x[[1]][which(x[[1]]>1))"
> x2<-x[[1]][which(x[[1]]>1))]
错误: 意外的')' in "x2<-x[[1]][which(x[[1]]>1))"
> x2<-x[[1]][which(x[[1]]>1)]
> x2
[1] "23.3"
> x1[[1]][x1[[1]]>1]
[1] 1.052632 1.473684 1.894737 2.315789 2.736842 3.157895 3.578947 4.000000
> which(x1[[4]]=="MED)
+ which(x1[[4]]=="MED")
错误: unexpected symbol in:
"which(x1[[4]]=="MED)
which(x1[[4]]=="MED"
> which(x1[[4]]=="MED")
[1] 2 4 5
> LIEBIAO<-list(facs=x1[[4]],nums=c(3,2.1,3.3,4,1.5,4.9),oldlist=x1[[c(1,2,3)]])
Error in x1[[c(1, 2, 3)]] : 递回索引在2层失败
> LIEBIAO<-list(facs=x1[[4]],nums=c(3,2.1,3.3,4,1.5,4.9),oldlist=x1[c(1,2,3)])
> LIEBIAO
$facs
[1] LOW  MED  LOW  MED  MED  HIGH
Levels: HIGH LOW MED

$nums
[1] 3.0 2.1 3.3 4.0 1.5 4.9

$oldlist
$oldlist[[1]]
 [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
 [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
[13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
[19]  3.5789474  4.0000000

$oldlist[[2]]
      [,1]  [,2]  [,3]
[1,] FALSE  TRUE  TRUE
[2,]  TRUE FALSE FALSE
[3,]  TRUE  TRUE FALSE

$oldlist[[3]]
[1] "Don"     "Quixote"


> LIEBIAO$facs[LIEBIAO$nums>=3]
[1] LOW  LOW  MED  HIGH
Levels: HIGH LOW MED
> LIEBIAO$flags<-c(rep(LIEBIAO$oldlist[[2]][,3],times=2))
> LIEBIAO
$facs
[1] LOW  MED  LOW  MED  MED  HIGH
Levels: HIGH LOW MED

$nums
[1] 3.0 2.1 3.3 4.0 1.5 4.9

$oldlist
$oldlist[[1]]
 [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
 [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
[13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
[19]  3.5789474  4.0000000

$oldlist[[2]]
      [,1]  [,2]  [,3]
[1,] FALSE  TRUE  TRUE
[2,]  TRUE FALSE FALSE
[3,]  TRUE  TRUE FALSE

$oldlist[[3]]
[1] "Don"     "Quixote"


$flags
[1]  TRUE FALSE FALSE  TRUE FALSE FALSE

> LIEBIAO$num[!LIEBIAO$flags[[1,3]]]
Error in LIEBIAO$flags[[1, 3]] : 下标数目不对
> LIEBIAO$num[LIEBIAO$flags[[1,3]]!=T]
Error in LIEBIAO$flags[[1, 3]] : 下标数目不对
> LIEBIAO$num[LIEBIAO$flags[1,3]!=T]
Error in LIEBIAO$flags[1, 3] : 量度数目不对
> LIEBIAO$num[LIEBIAO$flags[1,3]!=TRUE]
Error in LIEBIAO$flags[1, 3] : 量度数目不对
> LIEBIAO$nums[LIEBIAO$flags[1,3]!=TRUE]
Error in LIEBIAO$flags[1, 3] : 量度数目不对
> LIEBIAO$nums[LIEBIAO$flags!=TRUE]
[1] 2.1 3.3 1.5 4.9

> LIEBIAO$nums[!LIEBIAO$flags]
[1] 2.1 3.3 1.5 4.9
> LIEBIAO$oldlist[[3]]<-"Don Quixote"
> LIEBIAO
$facs
[1] LOW  MED  LOW  MED  MED  HIGH
Levels: HIGH LOW MED

$nums
[1] 3.0 2.1 3.3 4.0 1.5 4.9

$oldlist
$oldlist[[1]]
 [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
 [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
[13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
[19]  3.5789474  4.0000000

$oldlist[[2]]
      [,1]  [,2]  [,3]
[1,] FALSE  TRUE  TRUE
[2,]  TRUE FALSE FALSE
[3,]  TRUE  TRUE FALSE

$oldlist[[3]]
[1] "Don Quixote"


$flags
[1]  TRUE FALSE FALSE  TRUE FALSE FALSE

> dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=c("M","F","M","M","F","M"),funny=c("High","Med","Low","High","Med","Med"))
> dframe
    person sex funny
1     Stan   M  High
2 Francine   F   Med
3    Steve   M   Low
4    Roger   M  High
5   Haylay   F   Med
6   Klause   M   Med
> dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(c("M","F","M","M","F","M"),levels=("F","M")),funny=factor(x=c("High","Med","Low","High","Med","Med"),levels=("Low","Med","High")))
错误: 意外的',' in "dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(c("M","F","M","M","F","M"),levels=("F","
> dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(x=c("M","F","M","M","F","M"),levels=("F","M")),funny=factor(x=c("High","Med","Low","High","Med","Med"),levels=("Low","Med","High")))
错误: 意外的',' in "dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(x=c("M","F","M","M","F","M"),levels=("F","
> dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(x=c("M","F","M","M","F","M"),levels=("F","M")),funny=factor(x=c("High","Med","Low","High","Med","Med"),levels=("Low","Med","High")))
错误: 意外的',' in "dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(x=c("M","F","M","M","F","M"),levels=("F","
> dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(c("M","F","M","M","F","M")),funny=factor(x=c("High","Med","Low","High","Med","Med"),levels=("Low","Med","High")))
错误: 意外的',' in "dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(c("M","F","M","M","F","M")),funny=factor(x=c("High","Med","Low","High","Med","Med"),levels=("Low","
> dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(c("M","F","M","M","F","M")),funny=factor(c("High","Med","Low","High","Med","Med")))
> dframe
    person sex funny
1     Stan   M  High
2 Francine   F   Med
3    Steve   M   Low
4    Roger   M  High
5   Haylay   F   Med
6   Klause   M   Med
> dframe$sex
[1] M F M M F M
Levels: F M
> dframe$funny
[1] High Med  Low  High Med  Med 
Levels: High Low Med
> dframe$person
[1] Stan     Francine Steve    Roger    Haylay   Klause  
Levels: Francine Haylay Klause Roger Stan Steve
> dframe<-data.frame(person=c("Stan","Francine","Steve","Roger","Haylay","Klause"),sex=factor(c("M","F","M","M","F","M")),funny=factor(c("High","Med","Low","High","Med","Med")),stringsAsFactors=FALSE)
> dframe$person
[1] "Stan"     "Francine" "Steve"    "Roger"    "Haylay"   "Klause"  
> dframe$age<-c(41,41,15,1600,21,60)
> dframe
    person sex funny  age
1     Stan   M  High   41
2 Francine   F   Med   41
3    Steve   M   Low   15
4    Roger   M  High 1600
5   Haylay   F   Med   21
6   Klause   M   Med   60
> dframe$age
[1]   41   41   15 1600   21   60
> y<-data.frame(dframe$person,dframe$age,dframe$sex,dframe$funny)
> y
  dframe.person dframe.age dframe.sex dframe.funny
1          Stan         41          M         High
2      Francine         41          F          Med
3         Steve         15          M          Low
4         Roger       1600          M         High
5        Haylay         21          F          Med
6        Klause         60          M          Med
> y<-data.frame(person=dframe$person,age=dframe$age,sex=dframe$sex,funny=dframe$funny)
> y
    person  age sex funny
1     Stan   41   M  High
2 Francine   41   F   Med
3    Steve   15   M   Low
4    Roger 1600   M  High
5   Haylay   21   F   Med
6   Klause   60   M   Med
> y2<-y[-y$funny]
Error in `[.data.frame`(y, -y$funny) : undefined columns selected
此外: Warning message:
In Ops.factor(y$funny) : ‘-’ not meaningful for factors
> y2<-y[-4]
> y2
    person  age sex
1     Stan   41   M
2 Francine   41   F
3    Steve   15   M
4    Roger 1600   M
5   Haylay   21   F
6   Klause   60   M
> mydataframe<-cbind(y2,y)
> mydataframe
    person  age sex   person  age sex funny
1     Stan   41   M     Stan   41   M  High
2 Francine   41   F Francine   41   F   Med
3    Steve   15   M    Steve   15   M   Low
4    Roger 1600   M    Roger 1600   M  High
5   Haylay   21   F   Haylay   21   F   Med
6   Klause   60   M   Klause   60   M   Med
> mydataframe<-rbind(y2,y)
Error in rbind(deparse.level, ...) : 变量的列数不对
> mydataframe<-rbind(dframe,y)
> mydatafarme[funny=Med|funny=High,1:2]
错误: 意外的'=' in "mydatafarme[funny=Med|funny="
> mydatafarme[(funny=Med|funny=High),1:2]
错误: 找不到对象'mydatafarme'
> mydataframe[(funny=Med|funny=High),1:2]
Error in `[.data.frame`(mydataframe, (funny = Med | funny = High), 1:2) : 
  找不到对象'High'
> mydataframe[(funny="Med"|funny="High"),1:2]
Error in "Med" | funny = "High" : 赋值目标擴充到非语言对象
> mydataframe[(funny=="Med"|funny=="High"),1:2]
Error in `[.data.frame`(mydataframe, (funny == "Med" | funny == "High"),  : 
  找不到对象'funny'
> mydataframe[(mydatafram$funny=="Med"|mydataframe$funny=="High"),1:2]
Error in `[.data.frame`(mydataframe, (mydatafram$funny == "Med" | mydataframe$funny ==  : 
  找不到对象'mydatafram'
> mydataframe[(mydataframe$funny=="Med"|mydataframe$funny=="High"),1:2]
     person sex
1      Stan   M
2  Francine   F
4     Roger   M
5    Haylay   F
6    Klause   M
7      Stan   M
8  Francine   F
10    Roger   M
11   Haylay   F
12   Klause   M

今日的学习感悟

学习一种语言,便是专心投入并积极主动的使用。无法静心使我不能分析所面对的事物,切记!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你介绍一些常用的数据操作。数据是Pandas库中最常用的数据结构之一,在数据分析和处理中非常重要。 以下是一些常用的数据操作: 1. 读写数据 读取CSV文件: ```python import pandas as pd df = pd.read_csv('data.csv') ``` 读取Excel文件: ```python import pandas as pd df = pd.read_excel('data.xlsx') ``` 写入CSV文件: ```python df.to_csv('data.csv', index=False) ``` 写入Excel文件: ```python df.to_excel('data.xlsx', index=False) ``` 2. 查看数据 查看前n行: ```python df.head(n) ``` 查看后n行: ```python df.tail(n) ``` 查看数据的行数和列数: ```python df.shape ``` 查看数据的列名: ```python df.columns ``` 查看数据的索引: ```python df.index ``` 查看数据数据类型: ```python df.dtypes ``` 3. 数据的子集操作 选择单列: ```python df['列名'] ``` 选择多列: ```python df[['列名1', '列名2', ...]] ``` 选择行: ```python df.loc[行索引] ``` 选择多行: ```python df.loc[行索引1:行索引2] ``` 选择行和列: ```python df.loc[行索引, ['列名1', '列名2', ...]] ``` 根据条件选择行: ```python df.loc[df['列名'] > 10] ``` 4. 数据的基本统计操作 计算列的均值: ```python df['列名'].mean() ``` 计算列的中位数: ```python df['列名'].median() ``` 计算列的最大值: ```python df['列名'].max() ``` 计算列的最小值: ```python df['列名'].min() ``` 计算列的标准差: ```python df['列名'].std() ``` 计算列的方差: ```python df['列名'].var() ``` 计算列的总和: ```python df['列名'].sum() ``` 5. 数据数据清洗操作 去重: ```python df.drop_duplicates() ``` 删除缺失值: ```python df.dropna() ``` 填充缺失值: ```python df.fillna(value) ``` 替换某列的值: ```python df.replace({'列名1': '旧值', '列名2': '旧值'}, {'列名1': '新值', '列名2': '新值'}) ``` 6. 数据数据变换操作 添加一列: ```python df['新列名'] = ... ``` 删除一列: ```python df.drop(['列名'], axis=1) ``` 重命名一列: ```python df.rename(columns={'旧列名': '新列名'}, inplace=True) ``` 排序: ```python df.sort_values(by='列名', ascending=False) ``` 分组: ```python df.groupby('列名') ``` 聚合: ```python df.groupby('列名').agg({'列名1': 'mean', '列名2': 'max', ...}) ``` 以上是一些常用的数据操作,希望能对你有所帮助。当然,除了这些操作,Pandas库还提供了更多强大的功能,可以根据具体需求进行使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值