任务一:读取mtcar数据并实现以下操作
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
print('mtcars的元素个数为:\n',mtcars.size)
print('mtcars的元素维度为:\n',mtcars.ndim)
print('mtcars的元素大小为:\n',mtcars.shape)
print('mtcars的表的列名为:\n',mtcars.columns)
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
print('对整个mtcar数据集进行描述性统计为:\n',mtcars.describe())
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
x = mtcars.loc[:,['cyl','carb','mpg','hp']]
y = x.groupby(['cyl','carb']).mean()
print('不同cyl(气缸数)、carb(化油器)对应的mpg(油耗)和hp(马力)的均值为:\n',y)
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
x = mtcars[['mpg','hp']][:5]
print('mpg和hp前5个元素为:\n',x)
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
x = mtcars[:][1:3]
print('mtcars数据的前3行元素为:\n',x)
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
print('mtcars数据的前5行元素为:\n',mtcars.head())
print('mtcars数据的后5行元素为:\n',mtcars.tail())
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
print('mtcar数据的第1列和第3列的数据为:\n',mtcars.loc[:,['mpg','disp']])
print('mtcar数据的第1列和第3列的数据为:\n',mtcars.iloc[:,[1,3]])
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
print('列名为mpg、hp,行名为2,3,4的数据为:\n',mtcars.loc[2:4,['mpg','hp']])
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
print('列位置为2和4,行位置为5,6,7的数据为:\n',mtcars.iloc[5:8,[2,4]]) #前闭后开区间
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
print('列位置为3,行名为2-6的数据为:\n',mtcars.loc[2:6,['disp']])
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
mtcars['abc']=mtcars['mpg']+mtcars['hp']
print('输出前5行数据为:\n',mtcars.head())
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
print('删除前两行数据前的长度为:\n',len(mtcars))
mtcars.drop(labels=range(0,2),axis=0,inplace=True)
print('删除前两行数据后的长度为:\n',len(mtcars))
print('前五行数据为:\n',mtcars.head())
程序代码如下
import pandas as pd
mtcars = pd.read_csv('F:\\mtcars.csv')
mtcars['abc']=mtcars['mpg']+mtcars['hp']
print('删除abc前列索引为:\n',mtcars.columns)
mtcars.drop(labels='abc',axis=1,inplace=True)
print('删除abc后列索引为:\n',mtcars.columns)