Python用pandas 处理 CSV文件 三

Python用pandas 处理 CSV文件 三

编码文章call10242025-03-27 9:52:1620A+A-

回顾一下前面CSV处理的知识

文章 1 Python处理CSV文件

文章2 Python处理CSV 之 二

pandas工具作为数据分析中的利器也可以处理CSV文件


  • 安装pandas
 pip install pandas
  • 创建一个CSV文件 内容如下,这是一批刚入职的员工信息
Name,Hire Date,Salary,Sick Days remaining
Graham Chapman,03/15/14,50000.00,10
John Cleese,06/01/15,65000.00,8
Eric Idle,05/12/14,45000.00,10
Terry Jones,11/01/13,70000.00,3
Terry Gilliam,08/12/14,48000.00,7
Michael Palin,05/23/13,66000.00,8


读取CSV文件

import pandas
df = pandas.read_csv('hrdata.csv')
print(df)
           Name Hire Date   Salary  Sick Days remaining
0  Graham Chapman  03/15/14  50000.0                   10
1     John Cleese  06/01/15  65000.0                    8
2       Eric Idle  05/12/14  45000.0                   10
3     Terry Jones  11/01/13  70000.0                    3
4   Terry Gilliam  08/12/14  48000.0                    7
5   Michael Palin  05/23/13  66000.0                    8

可以看到每一行有一个索引,从 0 开始到行数-1

获取所有的姓名

print(df["Name"])
0    Graham Chapman
1       John Cleese
2         Eric Idle
3       Terry Jones
4     Terry Gilliam
5     Michael Palin
Name: Name, dtype: object

指定索引

import pandas
df = pandas.read_csv('hrdata.csv', index_col='Name')
print(df)
# print(df["Name"])  会报错
print(df["Salary"])
  • print(df) 的结果: index_col='Name' 指定了索引列此时每一行的数字变成了Name
               Hire Date   Salary  Sick Days remaining
Name                                                  
Graham Chapman  03/15/14  50000.0                   10
John Cleese     06/01/15  65000.0                    8
Eric Idle       05/12/14  45000.0                   10
Terry Jones     11/01/13  70000.0                    3
Terry Gilliam   08/12/14  48000.0                    7
Michael Palin   05/23/13  66000.0                    8

print(df["Name"] 会报错:

print(df["Name"])
  • print(df["Salary"])


Name
Graham Chapman    50000.0
John Cleese       65000.0
Eric Idle         45000.0
Terry Jones       70000.0
Terry Gilliam     48000.0
Michael Palin     66000.0
Name: Salary, dtype: float64

获取第一个人的往期时间

print(df['Hire Date'][0])
  • df['Hire Date'] 取日期列的所有数字变成一个数组
  • df['Hire Date'][0] 取数组的第一个元素
在Excelt很容易表示这个过程,选选中第二列的所有数据,然后取第一个

读取CSV中部分列


import pandas
df = pandas.read_csv('hrdata.csv', 
            index_col='Employee', 
            parse_dates=['Hired'], 
            header=0, 
            names=['Employee', 'Hired','Salary', 'Sick Days'])
print(df)
                    Hired   Salary  Sick Days
Employee                                     
Graham Chapman 2014-03-15  50000.0         10
John Cleese    2015-06-01  65000.0          8
Eric Idle      2014-05-12  45000.0         10
Terry Jones    2013-11-01  70000.0          3
Terry Gilliam  2014-08-12  48000.0          7
Michael Palin  2013-05-23  66000.0          8
  • index_col='Employee', Employee实际上代码的是 Name的值
  • parse_dates=['Hired'], 处理第二列日期
  • header=0, 表示第0行是标题头,剩下行是数据
  • names=['Employee', 'Hired','Salary', 'Sick Days']) 要显示的列名


点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4