1、DataFrame的常用操作
1)查看DataFrame的大小
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
print(df.shape)
2)查看DataFrame的列名(columns)
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
print(df.columns)
3)查看DataFrame的行名(index)
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
print(df.index)
4)查看每行的数据类型
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
print(df.dtypes)
5)查看行列的内容
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
print(df)
print(df.iloc[-1]) #选取DataFrame最后一行,返回的是Series
print(df.iloc[-1:]) #选取DataFrame最后一行,返回的是DataFrame
print(df.loc[0:1,'b':'c']) #这种用于选取行索引列索引已知
print(df.iat[1,1]) #选取第二行第二列,用于已知行、列位置的选取。
print(df.head(2)) #查看前两行的内容
print(df.tail(2)) #查看后两行的内容
print(df.loc[[df.index[0],df.index[1]]]) #通过label查看
print(df.loc[[df.index[-2],df.index[-1]]])
6)查看每一列数据的整体特征
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
print(df.describe())
7)对DataFrame进行倒置
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
print(df.T)
2、DataFrame的行列运算
1)DataFrame中两列数据相加减
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
df['d - a'] = df['d'] - df['a']
df['d + a'] = df['d'] + df['a']
print(df)
2)DataFrame中两行数据相加减
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
print(df[df.a==10].values-df[df.a==1].values)
print(df[df.a==10].values+df[df.a==1].values)
相关文档:
Python pandas.DataFrame.iloc函数方法的使用
Python pandas.DataFrame.loc函数方法的使用
Python pandas dataframe iloc 和 loc 的用法及区别
Python pandas.DataFrame.at函数方法的使用
Python pandas.DataFrame.iat函数方法的使用