本文主要是介绍Python - pandas DataFrame创建、数据提取(loc、iloc),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 1 新建一个DataFrame表
- 2 获取列名
- 2.1 list(df)
- 2.2 .columns
- 3 通过[]获取指定行或指定列的数据
- 3.1 获取指定行(单行或多行,DataFrame)
- 3.2 获取指定列(单列或多列,Series/DataFrame)
- 4 通过.loc[]获取指定行、列的数据
- 4.1 获取单个数据(类型依数据本身)
- 4.2 获取单行数据(Series/DataFrame)
- 4.3 获取单列数据(Series/DataFrame)
- 4.3 获取多行多列的数据(DataFrame)
- 4.4 根据列值条件获取指定数据(Series/DataFrame)
- 5 通过.iloc[]用数字定位获取指定行、列的数据
- 5.1 获取单个数据(类型依数据本身)
- 5.2 获取单行数据(Series/DataFrame)
- 5.2 获取单列数据(Series/DataFrame)
- 5.3 获取多行多列的数据(DataFrame)
- 6 复制一个表的数据到另一个表
1 新建一个DataFrame表
import numpy as np
import pandas as pd
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])print(df)
name gender age
0 Snow M 22
1 Tyrion M 32
2 Sansa F 18
3 Arya F 14
2 获取列名
2.1 list(df)
代码示例:
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
cols = list(df)
print(cols)
print(type(cols))
['name', 'gender', 'age']
<class 'list'>
2.2 .columns
代码示例:
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
cols = df.columns
print(cols)
print(type(cols))
Index(['name', 'gender', 'age'], dtype='object')
<class 'pandas.core.indexes.base.Index'>
3 通过[]获取指定行或指定列的数据
3.1 获取指定行(单行或多行,DataFrame)
格式:
# 获取第row_start_index行到第row_end_index-1行的数据,返回值为DataFrame类型
df[row_start_index:row_end_index]
代码示例:
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df[1:2]# 获得第1行的数据
print(df_1,'\n',type(df_1))
name gender age
1 Tyrion M 32
<class 'pandas.core.frame.DataFrame'>
3.2 获取指定列(单列或多列,Series/DataFrame)
格式:
# 获取单列,返回值为Series类型
df['column_name']
# 获取单列,返回值为DataFrame类型
df[['column_name']]
# 获取多列,返回值为DataFrame类型
df[['column_name1','column_name2',...]]
代码示例:
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
series_1 = df['name']
df_1 = df[['name']]
df_2 = df[['name','gender']]
print(series_1,'\n',type(series_1),'\n',df_1,'\n',type(df_1),'\n',df_2,'\n',type(df_2))
0 Snow
1 Tyrion
2 Sansa
3 Arya
Name: name, dtype: object <class 'pandas.core.series.Series'> name
0 Snow
1 Tyrion
2 Sansa
3 Arya <class 'pandas.core.frame.DataFrame'> name gender
0 Snow M
1 Tyrion M
2 Sansa F
3 Arya F <class 'pandas.core.frame.DataFrame'>
4 通过.loc[]获取指定行、列的数据
4.1 获取单个数据(类型依数据本身)
格式:
# 返回值类型依数据本身
df.loc[index,column_name]
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df.loc[0,"name"]
print(df_1,'\n',type(df_1))
Snow <class 'str'>
4.2 获取单行数据(Series/DataFrame)
格式:
# 返回值为Series类型
df.loc[index]
# 返回值为DataFrame类型
df.loc[index:index]
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df.loc[0]
df_2 = df.loc[0:0]
print(df_1,'\n',type(df_1),'\n',df_2,'\n',type(df_2))
name Snow
gender M
age 22
Name: 0, dtype: object
<class 'pandas.core.series.Series'> name gender age
0 Snow M 22
<class 'pandas.core.frame.DataFrame'>
4.3 获取单列数据(Series/DataFrame)
格式:
# 返回值为Series类型
df.loc[:,column_name]
# 返回值为DataFrame类型
df.loc[:,[column_name]]
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df.loc[:,"name"]
df_2 = df.loc[:,["name"]]
print(df_1,'\n',type(df_1),'\n',df_2,'\n',type(df_2))
0 Snow
1 Tyrion
2 Sansa
3 Arya
Name: name, dtype: object
<class 'pandas.core.series.Series'> name
0 Snow
1 Tyrion
2 Sansa
3 Arya
<class 'pandas.core.frame.DataFrame'>
4.3 获取多行多列的数据(DataFrame)
格式:
# 获取第start_index行到end_index行,'column_name1','column_name2',...列的数据
df.loc[start_index:end_index,['column_name1','column_name2',...]]
# 获取index1,index2,...行,'column_name1','column_name2',...列的数据
df.loc[[index1,index2,...],['column_name1','column_name2',...]]
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df.loc[0:2, ['name', 'age']] # 获取第0行到第2行,name列和age列的数据
df_2 = df.loc[[2, 3], ['name', 'age']] # 获取指定的第2行和第3行,name和age列的数据
print(df_1,'\n',type(df_1),'\n',df_2,'\n',type(df_2))
name age
0 Snow 22
1 Tyrion 32
2 Sansa 18 <class 'pandas.core.frame.DataFrame'> name age
2 Sansa 18
3 Arya 14 <class 'pandas.core.frame.DataFrame'>
4.4 根据列值条件获取指定数据(Series/DataFrame)
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df.loc[df['gender']=='M',:] #获取gender列值是M的全部数据
df_2 = df.loc[df['gender']=='M','name'] #获取gender列值是M,name列的数据
df_3 = df.loc[df['gender']=='M',['name']] #获取gender列值是M,name列的数据
df_4 = df.loc[df['gender']=='M',['name','age']] #获取gender列值是M,name和age列的数据
print(df_1,'\n',type(df_1),'\n',df_2,'\n',type(df_2),'\n',df_3,'\n',type(df_3),'\n',df_4,'\n',type(df_4))
name gender age
0 Snow M 22
1 Tyrion M 32 <class 'pandas.core.frame.DataFrame'> 0 Snow
1 Tyrion
Name: name, dtype: object <class 'pandas.core.series.Series'> name
0 Snow
1 Tyrion <class 'pandas.core.frame.DataFrame'> name age
0 Snow 22
1 Tyrion 32 <class 'pandas.core.frame.DataFrame'>
5 通过.iloc[]用数字定位获取指定行、列的数据
在column_name特别长或者index是时间序列等各种不方便输入的情况下,可以用iloc[row_index, column_index],它完全用数字来定位。
5.1 获取单个数据(类型依数据本身)
格式:
df.iloc[indexX,indexY]
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df.iloc[0, 0] # 获取第0行第0列的数据,'Snow'
df_2 = df.iloc[1, 2] # 获取第1行第2列的数据,32
print(df_1,'\n',type(df_1),'\n',df_2,'\n',type(df_2))
Snow
<class 'str'>
32
<class 'numpy.int64'>
5.2 获取单行数据(Series/DataFrame)
格式:
# 返回值为Series类型
df.iloc[index]
# 返回值为DataFrame类型
df.iloc[index:index+1]
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df.iloc[0]
df_2 = df.iloc[0:1]
print(df_1,'\n',type(df_1),'\n',df_2,'\n',type(df_2))
name Snow
gender M
age 22
Name: 0, dtype: object
<class 'pandas.core.series.Series'> name gender age
0 Snow M 22
<class 'pandas.core.frame.DataFrame'>
5.2 获取单列数据(Series/DataFrame)
格式:
# 返回值为Series类型
df.iloc[:,index]
# 返回值为DataFrame类型
df.iloc[:,index:index+1]
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df.iloc[:,0]
df_2 = df.iloc[:,0:1]
print(df_1,'\n',type(df_1),'\n',df_2,'\n',type(df_2))
0 Snow
1 Tyrion
2 Sansa
3 Arya
Name: name, dtype: object <class 'pandas.core.series.Series'> name
0 Snow
1 Tyrion
2 Sansa
3 Arya <class 'pandas.core.frame.DataFrame'>
5.3 获取多行多列的数据(DataFrame)
格式:
# 获取第start_index行到end_index-1行,index1,index2,...列的数据
df.iloc[start_index:end_index,[index1,index2,...]]
# 获取index1,index2,...行,第start_index行到end_index-1列的数据
df.iloc[[index1,index2,...],start_index:end_index]
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
df_1 = df.iloc[[1, 3], 0:1] # 获取第1行和第3行,第0列的数据
df_2 = df.iloc[1:3, [1, 2]] # 获取第1行到第2行,第1列和第2列的数据
print(df_1,'\n',type(df_1),'\n',df_2,'\n',type(df_2))
name
1 Tyrion
3 Arya
<class 'pandas.core.frame.DataFrame'> gender age
1 M 32
2 F 18
<class 'pandas.core.frame.DataFrame'>
6 复制一个表的数据到另一个表
data_copy = data.copy()
这篇关于Python - pandas DataFrame创建、数据提取(loc、iloc)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!