在处理包含字符串的数据时使用pandas
常用的数据类型:1.series一维,带标签数组
2.dataframe二维,Series容器
import pandas as pd
t = pd.Series([1, 21, 31, 51, 852])
print(t)
print("*" * 100)
# 指定索引
t2 = pd.Series([1, 23, 2, 2, 1], index=list("abcde")).astype(float)
print(t2)
print("*" * 100)
# 字典的“键”自动变为索引
temp_dict = {"name": "xiaoming", "age": 18, "tel": 13555}
t3 = pd.Series(temp_dict)
print(t3)
print(t3.dtype)
print(t3["age"])
print(t3[1])
print(t3[[0, 2]])
print(t3[["name", "age"]])
print(t3.index)
print(list(t3.index)[:2])
pandas读取csv文件
import pandas as pd
#pandas读取csv文件
df = pd.read_csv("./dogNames2.csv")
print(df)
from pymongo import MongoClient
import pandas as pd
client = MongoClient()
collection = client["douban"]["tv1"]
data = list(collection.find())
t1 = data[0]
t1 = pd.Series(t1)
import pandas as pd
import numpy as np
t0 = pd.DataFrame(np.arange(12).reshape(3,4),index=list("abc"),columns=list("wxyz"))
print(t0)
d1 = {"name":["xiaoming","xiaohong"],"age":[20,30],"tel":[10086,10010]}
t1 = pd.DataFrame(d1)
print(t1)
d2 = [{"name":"xiaoming","age":20,"tel":10086},{"name":"xiaohong","age":52,"tel":11225},{"name":"zhangliang","age":15,"tel":56321}]
t2 = pd.DataFrame(d2)
print(t2)
import pandas as pd
import numpy as np
# #pandas读取csv文件
# df = pd.read_csv("./dogNames2.csv")
# # print(df)
# print(df.head())
# print(df.tail())
# #DataFrame中排序的方法
# df = df.sort_values(by="Count_AnimalName",ascending=False) #按照某一列进行排序
# print(df.head()) #默认是5行
# print("*"*100)
# print(df.tail()) #默认是5行
#
# # pandas取行或者列的注意点
# # ——方括号写数组,表示取行,对行进行操作
# # ——写字符串表示取列索引,对列进行操作
# print(df[:20]["Row_Labels"])
import pandas as pd
import numpy as np
t0 = pd.DataFrame(np.arange(12).reshape(3,4),index=list("abc"),columns=list("wxyz"))
print(t0)
print("*"*100)
print(t0.loc["a","z"]) #"a"行,“z"列
print("*"*100)
print(t0.loc[["a","b"],:]) #”a“、"b" 两行
print("*"*100)
print(t0.loc[["a","b"],["w","x"]]) #”a“、"b" 两行 , ”w“、"x" 两行
print("*"*100)
print(t0.iloc[1]) #第2行
print("*"*100)
print(t0.iloc[:,2]) #第3列
print(t0.iloc[:,[2,1]]) #第3列,第2列
print("*"*100)
print(t0.iloc[1:,:2]) #第1行之后的每一行,第2列之前的每一列
import pandas as pd
#pandas读取csv文件
df = pd.read_csv("./dogNames2.csv")
# 布尔索引
print(df[(800
import pandas as pd
import numpy as np
file_path = "IMDB-Movie-Data.csv"
#pandas读取csv文件
df = pd.read_csv(file_path)
# 获取目录信息
print(df.info())
# 获取平均分数
print(df["Rating"].mean())
# 获取导演人数
print(len(set(df["Director"].tolist())))
print(len(df["Director"].unique()))
# 获取演员的人数
temp_Actors_list = df["Actors"].str.split(",").tolist()
actors_list = [i for j in temp_Actors_list for i in j]
actors_num = len(set(actors_list))
print(actors_num)
import pandas as pd
from matplotlib import pyplot as plt
file_path = "./IMDB-Movie-Data.csv"
df = pd.read_csv(file_path)
print(df.head())
print(df.info())
# rating,runtime分布情况
#选择图形
runtime_data = df["Runtime (Minutes)"].values
max_runtime = runtime_data.max()
min_runtime = runtime_data.min()
#计算组数
print(max_runtime-min_runtime)
num_bin = (max_runtime-min_runtime)//5
#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.hist(runtime_data,num_bin)
plt.xticks(range(min_runtime,max_runtime+5,5))
plt.show()