mean]print("---------------")print(new_df)#使用bool获取数据print("_dataframe 过滤">
当前位置:   article > 正文

DataFrame的条件过滤_dataframe 过滤

dataframe 过滤

Series向量的操作

from pandas import read_csv

# 加载文件
df = read_csv("scientists.csv")
print(df)
print(type(df))

# 得到年龄这一列
age = df["Age"]
print(age)
print("***************")
print(age + 100)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

DataFrame的条件过滤

from pandas import read_csv

# 加载文件
df = read_csv("scientists.csv", sep=",")
print(df)
print(type(df))

# 得到寿命大于平均值的数据
mean = df["Age"].mean()  # 平均年龄
print(mean)

new_df = df[df["Age"] > mean]
print("---------------")
print(new_df)

#使用bool获取数据
print("---------")
print(df[[True,True,True,False,False,True,False,False]])

#使用loc或者iloc根据下标获取数据
print("loc")
print(df.loc[[0,1,2,5,7]])
print("iloc")
print(df.iloc[[0,1,2,5,-1]])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述


DataFrame添加修改列

from pandas import read_csv,to_datetime

df = read_csv("scientists.csv")

print(df)

old_Born = df["Born"]
print(old_Born)
print(type(old_Born))

#转换类型
new_born = to_datetime(old_Born)
print("转换类型")
print(new_born)
print(type(new_born))

#添加新的列
df["new_born"] = new_born
print("添加新的列")
print(df)

#修改数据
df["new_born"] = ["n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8"]
print("修改数据")
print(df)

#删除列
#axis=1是删除列,需要写上列名
result = df.drop(["new_born", "Age", "Died"],axis=1)
print(result)
print("#删除列")
print(result)

#axis=0是删除行,需要写上上行的下标索引
result = df.drop([0,2,6],axis=0)
print(result)


在这里插入代码片
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在这里插入图片描述
csv数据

Name,Born,Died,Age,Occupation
Rosaline Franklin,1920-07-25,1958-04-16,37,Chemist
William Gosset,1876-06-13,1937-10-16,61,Statistician
Florence Nightingale,1820-05-12,1910-08-13,90,Nurse
Marie Curie,1867-11-07,1934-07-04,66,Chemist
Rachel Carson,1907-05-27,1964-04-14,56,Biologist
John Snow,1813-03-15,1858-06-16,45,Physician
Alan Turing,1912-06-23,1954-06-07,41,Computer Scientist
Johann Gauss,1777-04-30,1855-02-23,77,Mathematician
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/600981
推荐阅读
相关标签
  

闽ICP备14008679号