目录
- 1、DataFrame是什么
- 2、创建一个dataframe
- 3、获取dataframe的行、列索引
- 4、获取dataframe的值
1、DataFrame是什么
series是有一组数据与一组索引(行索引)组成的数据结构,而dataframe是由一组数据与一对索引(行索引和列索引)组成的表格型数据结构。之所以叫表格型数据结构,是因为dataframe的数据形式和Excel的数据存储形式很相近。
2、创建一个dataframe
创建一个dataframe使用的方法是pd.dataframe(),通过给dataframe()方法传入不同的对象即可实现。
import pandas as pd
df1 = pd.DataFrame(["a","b","c","d"])
df2 = pd.DataFrame([["a","A"],["b","B"],["c","C"],["d","D"]])
df31 = pd.DataFrame([["a","A"],["b","B"],["c","C"],["d","D"]],columns = ["小写","大写"])
df32 = pd.DataFrame([["a","A"],["b","B"],["c","C"],["d","D"]],index = ["一","二","三","四"])
df33 = pd.DataFrame([["a","A"],["b","B"],["c","C"],["d","D"]],columns = ["小写","大写"],index = ["一","二","三","四"])
print(df1)
print(df2)
print(df31)
print(df32)
print(df33)
0
0 a
1 b
2 c
3 d
0 1
0 a A
1 b B
2 c C
3 d D
小写 大写
0 a A
1 b B
2 c C
3 d D
0 1
一 a A
二 b B
三 c C
四 d D
小写 大写
一 a A
二 b B
三 c C
四 d D
总结:
1.只传入一个单一列表时,该列表的值会显示成一列,且行和列都是从0列开始的默认索引。
2.当传入一个嵌套列表时,会根据嵌套列表数显示成多列数据,行、列索引同样是从0开始的默认索引。列表里面嵌套的列表也可以换成元组。也可以手动设置行列索引
import pandas as pd
df41 = pd.DataFrame({"小写":["a","b","c","d"],"大写":["A","B","C","D"]})
df42 = pd.DataFrame({"小写":["a","b","c","d"],"大写":["A","B","C","D"]},index = ["一","二","三","四"])
print(df41)
print(df42)
小写 大写
0 a A
1 b B
2 c C
3 d D
小写 大写
一 a A
二 b B
三 c C
四 d D
总结:直接以字典的形式传入dataframe时,字典的key值就相当于列索引,如果没有设置行索引,行索引还是使用从0开始的默认索引,同样可以使用index参数自定义行索引。
3、获取dataframe的行、列索引
import pandas as pd
df41 = pd.DataFrame({"小写":["a","b","c","d"],"大写":["A","B","C","D"]})
df42 = pd.DataFrame({"小写":["a","b","c","d"],"大写":["A","B","C","D"]},index = ["一","二","三","四"])
print(df41.columns)
print(df42.columns)
print(df41.index)
print(df42.index)
Index(['小写', '大写'], dtype='object')
Index(['小写', '大写'], dtype='object')
RangeIndex(start=0, stop=4, step=1)
Index(['一', '二', '三', '四'], dtype='object')
4、获取dataframe的值
通过列表的形式获取某一行或某一列或某几行或某几类。
import pandas as pd
import numpy as np
date =pd.date_range('20160101',periods=6)
# print(date)
df = pd.DataFrame(np.arange(24).reshape((6,4)),index=date,columns=['A','B','C','D'])
print(df['A'],df.A)
print(df[0:3])
print(df['2016-01-01':'2016-01-03'])
2016-01-01 0
2016-01-02 4
2016-01-03 8
2016-01-04 12
2016-01-05 16
2016-01-06 20
Freq: D, Name: A, dtype: int32 2016-01-01 0
2016-01-02 4
2016-01-03 8
2016-01-04 12
2016-01-05 16
2016-01-06 20
Freq: D, Name: A, dtype: int32
A B C D
2016-01-01 0 1 2 3
2016-01-02 4 5 6 7
2016-01-03 8 9 10 11
A B C D
2016-01-01 0 1 2 3
2016-01-02 4 5 6 7
2016-01-03 8 9 10 11
通过select by label: loc
import pandas as pd
import numpy as np
date =pd.date_range('20160101',periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)),index=date,columns=['A','B','C','D'])
print(df.loc['2016-01-02'])
print(df.loc[:,['A','B']])
print(df.loc['2016-01-02',['A','B']])
A 4
B 5
C 6
D 7
Name: 2016-01-02 00:00:00, dtype: int32
A B
2016-01-01 0 1
2016-01-02 4 5
2016-01-03 8 9
2016-01-04 12 13
2016-01-05 16 17
2016-01-06 20 21
A 4
B 5
Name: 2016-01-02 00:00:00, dtype: int32
通过select by position: iloc
import pandas as pd
import numpy as np
date =pd.date_range('20160101',periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)),index=date,columns=['A','B','C','D'])
print(df.iloc[3:5,1:3])
print(df.iloc[[1,3,5],1:3])
B C
2016-01-04 13 14
2016-01-05 17 18
B C
2016-01-02 5 6
2016-01-04 13 14
2016-01-06 21 22
通过boolean indexing
import pandas as pd
import numpy as np
date =pd.date_range('20160101',periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)),index=date,columns=['A','B','C','D'])
df.iloc[2,2] =111
df.loc['2016-01-01','B'] =222
df.C[df.C>10] =0
df.B[df.A>16] =0
#df[df.D>6] =0
df['E'] =pd.Series([1,2,3,4,5,6],index=pd.date_range('2016-01-01',periods=6))
df['F']=np.nan
print(df)
标签:01,df,DataFrame,数据结构,pd,print,2016,Pandas
From: https://blog.csdn.net/weixin_43597208/article/details/142335422