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)
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]])
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)
在这里插入代码片
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