【稳住,can win】Pandas 操作数据

*
Pandas 对象的创建

+ Series 一维数据
+ DataFrame 二维数据
DataFrame 对象索引
处理缺失数据

Pandas 对象的创建

Series 一维数据

t1 = pd.Series(np.arange(0, 10, 2), index=list("abcde"))

t2 = {"name":"xiaohong", "age":30, "tel":10086}
t2 = pd.Series(t2)

DataFrame 二维数据

t4 = {"name":["xiaoming", "xiaohong"], "age":[18, 20], "tel":[10088, 10089]}
t4 = pd.DataFrame(t4)

t5 = [{"name":"xiaoming", "age":18, "tel":10088}, {"name":"xiaohong", "age":20, "tel":10089}]
t5 = pd.DataFrame(t5)

DataFrame 对象索引

通过列名可以获取所有数据的某一属性。如下代码实现的是读取所有数据的 Actors属性。

file_path = "DataAnalysis/IMDB-Movie-Data.csv"
df = pd.read_csv(file_path)
print(df["Actors"])

【稳住,can win】Pandas 操作数据
bool索引同样在Pandas中也可以使用。
print(df[df["Row_Labels"].str.len()>4].head(10))

【稳住,can win】Pandas 操作数据

处理缺失数据

通过Pandas的isnull可以判断数据是否为nan。

【稳住,can win】Pandas 操作数据
对于nan数据,要么删除,要么填充为合适的数据。

【稳住,can win】Pandas 操作数据
上图将nan填充为同一列每行的均值。删除nan的时候,制定 how=any,一条数据只要有一个nan就删除。

Original: https://blog.csdn.net/caoDanLife/article/details/121897760
Author: Schuyler Hu
Title: 【稳住,can win】Pandas 操作数据

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/674210/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球