• 周二. 4月 30th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

DataFrame

admin

11月 28, 2021

一、介绍

DataFrame是一个表格型的数据结构,含有一组有序的列。DataFrame可以被看做是Series组成的字典,并且共用一个索引

二、案例

1、创建方式

import pandas as pd
print("创建方式:")
print("方法一:通过字典的方式进行创建:")
sr=pd.DataFrame({'one':[1,2,3],'two':[4,5,6]},index=['a','b','c'])
print(sr)
print("方法二:分别设置索引:")
sr1=pd.DataFrame({'one':pd.Series([1,2,3],index=['a','b','c']),'two':pd.Series([3,4,5,6],index=['d','a','b','c'])})
print(sr1)

print("从文件读取CSV:")
sr3=pd.read_csv(r'F:Python Workdata_analysisdata	est.csv')
print(sr3)
print("保存数据到csv文件中:")
sr1.to_csv(r'F:Python Workdata_analysisdata	est1.csv')#sr1为保存的字典名  若有缺失值则自动保存为空
print("保存成功!")

 

 2、常用属性

print(sr1)
print(sr1.index)#获取行索引
print(sr1.columns)#获取列索引
print(sr1.values)#获取数值(二维数组)

print(sr1.T)#转置,行变列,列变行,由于nan为浮点类型,每一列保持统一字符型,所以有了小数位

print(sr1.describe())#对于每一列进行分析和总结
#count:值得数量
#mean:平均数
#std:标准差
#50%:中位数

#DataFrame索引和切片
print(sr3)
print("------------------")
#获取某一指定得数值
print(sr3.loc[1,'a'])
print("------------------")
#获取指定得某一行
print(sr3.loc[1,:])
print("------------------")
#获取指定得某一列
print(sr3.loc[:,'a'])
print("------------------")
#获取指定的某两行
print(sr3.loc[{1,2},:])
print("------------------")
#获取指定的某两列
print(sr3.loc[:,{'b','c'}])
print("------------------")

import numpy as np
sr1.loc['d','two']=np.nan
sr1.loc['c','one']=np.nan
#数据对齐和数据缺失处理
print(sr1)
print("------------------")
#为缺失值复制为0
print(sr1.fillna(0))
print("------------------")
#删除具有缺失值的数据:会删除整行
print(sr1.dropna())
print("------------------")
#只想删除行索引中都为空的行
print(sr1.dropna(how="all"))
print("------------------")
#删除具有缺失值的列
df=sr1.dropna(how="all")
print(df.dropna(axis=1))#axis为轴,0为行,1为列
print("------------------")

 3、pandas中其他常用方法

#其他常用的函数
print(df)
print("---------1---------")
#求列平均数
print(df.mean())
print("--------2----------")
#求行平均数
print(df.mean(axis=1))
print("--------3----------")
#求和同理,用sum函数
#排序
#按列进行排序(有缺失值的部分不参与排期)
print(df.sort_values(by="two"))
print("---------4---------")
#降序
print(df.sort_values(by="two",ascending=False))
print("---------5---------")
#按标签进行排序
print(df.sort_index(axis=1))
print("------------------")

《DataFrame》有3个想法
  1. Lorsque nous soupçonnons que notre femme ou notre mari a trahi le mariage, mais qu’il n’y a aucune preuve directe, ou que nous voulons nous inquiéter de la sécurité de nos enfants, surveiller leurs téléphones portables est également une bonne solution, vous permettant généralement d’obtenir des informations plus importantes..

  2. Good day! Do you know if they make any plugins
    to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords
    but I’m not seeing very good gains. If you know of
    any please share. Thank you! You can read similar text here:
    Scrapebox List

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注