【Python数据科学】读取文件read

it2022-05-05  204

#导包 >>>import pandas as pd #使用read_csv,通过读取csv文件构建dataframe #可以直接读取utf格式 >>>df=pd.read_csv('dataAnalyst_sql_utf.csv') #gbk格式需要解码 >>>df=pd.read_csv('dataAnalyst_sql.csv',encoding='gbk') #查看前5行 >>>df.head() #查看最后5行 >>>df.tail() positionIdcitycompanyIdfirstTypesecondTypeeducationindustryFieldpositionAdvantagepositionNamepositionLablessalaryworkYear50262582910北京3786开发/测试/运维类数据开发本科移动互联网,金融大牛团队,互联网金融,零食水果,灵活工时BI数据分析师['数据分析', '数据', 'BI', '分析师', '商业智能']15k-25k3-5年50272583183北京59239开发/测试/运维类软件开发本科金融五险一金,年底奖金大数据风控研发工程师['专家', '高级', '软件开发']15K-30K3-5年50281832950北京50702技术数据开发本科移动互联网,O2O期权高级数据技术专家['数据挖掘', '数据']30k-40k5-10年50292582349北京156832市场/商务/销售类销售不限金融周末双休/高提成/每月员工趴分析师助理/销售人员['顾问', '销售', '分析师']4k-6k不限50301757974北京1575技术高端技术职位本科移动互联网,数据服务大公司,高福利,互联网数据团队,机会多数据仓库建模工程师['数据仓库', '数据', '建模']15k-30k不限 #查看这个dataframe信息 >>>df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 5031 entries, 0 to 5030 Data columns (total 12 columns): positionId 5031 non-null int64 city 5031 non-null object companyId 5031 non-null int64 firstType 5027 non-null object secondType 5028 non-null object education 5031 non-null object industryField 5031 non-null object positionAdvantage 5031 non-null object positionName 5031 non-null object positionLables 5007 non-null object salary 5031 non-null object workYear 5031 non-null object dtypes: int64(2), object(10) memory usage: 471.7+ KB #更改列类型 #对象名.字段名.astype (‘ 目标数据类型’) 改变字段的数据类型 >>>df.positionId=df.positionId. astype('str') #切片多列 >>>df[['city','education']] cityeducation0上海硕士1上海本科2上海本科3上海本科4上海本科5上海本科.........5027北京本科5028北京本科5029北京不限5030北京本科

5031 rows × 2 columns

#切片单列 >>>df['city'] 0 上海 1 上海 2 上海 3 上海 4 上海 .. 5028 北京 5029 北京 5030 北京 Name: city, Length: 5031, dtype: object #新建一列,字段计算 df['new']=df.positionId+df.companyId #过滤条件 df[df.query('new>2000000').city=='北京'] 0 False 1 False 2 False 3 False 4 False 5 False ... 5016 False 5017 False 5018 False 5020 False 5021 False 5022 False 5023 False 5024 False 5026 True 5027 True 5029 True Name: city, Length: 4011, dtype: bool

最新回复(0)