一、DataFrame的创建
Pandas 的数据结构主要是:Series(一维数组),DataFrame(二维数组)。DataFrame是由索引和内容组成,索引既有行索引index又有列索引columns,如 内容,index=[],colunms=[] 这样的形式。以下介绍的他的几种创建方式:
1、创建空的DataFrame
import pandas as pd
data_df = pd.DataFrame()
print(data_df)
2、使用List 创建DataFrame?
a_list=[0,1,2,3,4]
b_list=["apple","banana","cup","desk","example"]
data_df2=pd.DataFrame(b_list,a_list)
print(data_df2)
3、使用字典创建DataFrame?
import pandas as pd
dict={"numeric":[0,1,2,3,4,5],"alpha":["A","B","C","D","E","F"]}
dict_df=pd.DataFrame(dict)
dict_df
4、使用数组创建带索引的DataFrame
dict={"numeric":[0,1,2,3,4,5],"alpha":["A","B","C","D","E","F"]}
index=[]
for i in range(len(dict["alpha"])):
index.append(i)
dict_df1=pd.DataFrame(dict,index=index)
dict_df1
5、从字典列表创建DataFrame
data = [{"one":1,"two":2,"three":3},{"one":"apple","two":"banana","C":"cup"}]
data_df = pd.DataFrame(data)
data_df
6、使用zip()函数创建DataFrame?
zip()方法用于合并两个列表
grade = ["A","B","C","D","E","F"]
name = ["张三","李丽","杨光","李波","张波","欧晓"]
data1 = list(zip(grade,name))
print(data1)
data1_df = pd.DataFrame(data1,columns=["grade","name"],index=[2,3,4,5,6,7])
print(data1_df)
7、从序列的字典创建DataFrame?
import pandas as pd
name = ["张三","李丽","杨光","李波","张波","欧晓"]
data2 = {
"chineses":pd.Series([98,89,97,87,91,90],index=name),
"math":pd.Series([98,87,96,79,80,83],index=name)
}
data2_df = pd.DataFrame(data2)
print(data2_df)
二、DataFrame中的一些常用属性
df.values 返回DataFrame中的数值; df.columns 返回DataFrame中的列索引; df.ndim 返回DataFrame的维数; df.shape 返回DataFrame的形状; df.dtypes 返回DataFrame中每一列元素的数据类型; df.size 返回DataFrame中元素的个数; df.index 返回DataFrame中的索引; df.T 返回DataFrame的转置结果;
|