Pandas基础(一)
import sys # 引入 sys 模块
list=[1,2,3,4]
it = iter(list) # 创建迭代器对象
while True:
try:
print (next(it))
except StopIteration:
sys.exit()
1
2
3
4
An exception has occurred, use %tb to see the full traceback.
SystemExit
#!/usr/bin/python3
import sys
def fibonacci(n): # 生成器函数 - 斐波那契
a, b, counter = 0, 1, 0
while True:
if (counter > n):
return
yield a
a, b = b, a + b
counter += 1
f = fibonacci(10) # f 是一个迭代器,由生成器返回生成
while True:
try:
print (next(f), end=" ")
except StopIteration:
sys.exit()
0 1 1 2 3 5 8 13 21 34 55
An exception has occurred, use %tb to see the full traceback.
SystemExit
#!/usr/bin/python3
# Filename: test.py
# 导入模块
import support
# 现在可以调用模块里包含的函数了
support.print_func("17")
Hello : 17
import fibo
fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
from fibo import fib, fib2
fib2(500)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
import using_name
我来自另一模块
import utils
print(f"当前模块名称: {__name__}") # 输出: __main__
print(f"导入的模块名称: {utils.__name__}") # 输出: utils
utils.helper() # 不会触发 utils.py 中的测试代码
当前模块名称: main
导入的模块名称: utils
这是辅助函数
import config
# 正确调用
# config.connect_to_db() # 输出:成功连接到数据库
# 1. 查看导入的是哪个文件
print("导入的文件路径:", config.__file__)
# 2. 查看 config 模块中所有不以下划线开头的属性
print("可用的属性:", [attr for attr in dir(config) if not attr.startswith('_')])
# 3. 查看模块的源码位置
import inspect
try:
print("源码位置:", inspect.getfile(config))
except:
print("无法获取源码位置")
导入的文件路径: C:\Users\Administrator\config.py
可用的属性: ['DEBUG', 'sys']
源码位置: C:\Users\Administrator\config.py
import os
print(os.getcwd())
C:\Users\Administrator
from zeep import Client
# 服务的 WSDL 地址(通常在服务地址后加 ?wsdl)
wsdl_url = 'http://xxxx/CamWCFServices/QueryService.svc?wsdl'
# 创建客户端
client = Client(wsdl_url)
# 2. (强烈推荐) 打印出服务的完整结构,查看所有可用的方法名和参数
# 这一步会列出诸如 Login, Logout, ChangePassword 等具体方法
# print("正在解析服务结构...")
#client.wsdl.dump()
# 调用服务中的方法(根据实际契约中的方法名调用)
# 假设 AuthenticationService 中有一个 Login 方法
# result = client.service.Login(username='your_username', password='your_password')
# 创建 UserProfile 对象(具体结构需要根据 WSDL 定义)
user_profile = client.get_type('ns2:UserProfile')()
# 设置 user_profile 的属性(需要知道具体有哪些字段)
user_profile.Name = "12333"
user_profile.Password = ".COM"
# 创建 QueryParameters 对象
oQueryParam = client.get_type('ns2:QueryParameters')()
# 创建 QueryParameter 数组(长度为1)
oQueryParam.Parameters = client.get_type('ns2:ArrayOfQueryParameter')()
# 创建第一个 QueryParameter 对象
param = client.get_type('ns2:QueryParameter')()
param.Name = "EquipmentName"
# 处理 NameField.Data 的 null 判断
# 假设 NameField.Data 的值,如果为 None 则设置为 None
# name_field_data = None # 这里替换为实际的 NameField.Data 值
param.Value = "XXXX"
# 将参数放入数组
oQueryParam.Parameters[0] = param
# 创建 QueryOptions 对象
oQueryOptions = client.get_type('ns2:QueryOptions')()
oQueryOptions.QueryType = 'User' # OM.QueryType.User 对应的字符串值
oQueryOptions.StartRow = 1
# 调用 Execute 方法
result = client.service.Execute(
userProfile=user_profile,
queryName="shEquipmentMaint_GetChildEquipments",
Parameters=oQueryParam,
options=oQueryOptions
)
# 处理返回结果
status = result['ExecuteResult'] # ResultStatus 类型
record_set = result['recordSet'] # RecordSet 类型
print(status)
---------------------------------------------------------------------------
gaierror Traceback (most recent call last)
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\urllib3\connection.py:204, in HTTPConnection._new_conn(self)
203 try:
--> 204 sock = connection.create_connection(
205 (self._dns_host, self.port),
206 self.timeout,
207 source_address=self.source_address,
208 socket_options=self.socket_options,
209 )
210 except socket.gaierror as e:
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\urllib3\util\connection.py:60, in create_connection(address, timeout, source_address, socket_options)
58 raise LocationParseError(f"'{host}', label empty or too long") from None
---> 60 for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
61 af, socktype, proto, canonname, sa = res
File C:\Program Files (x86)\Python3.13.12\Lib\socket.py:977, in getaddrinfo(host, port, family, type, proto, flags)
976 addrlist = []
--> 977 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
978 af, socktype, proto, canonname, sa = res
gaierror: [Errno 11001] getaddrinfo failed
The above exception was the direct cause of the following exception:
NameResolutionError Traceback (most recent call last)
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\urllib3\connectionpool.py:787, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
786 # Make the request on the HTTPConnection object
--> 787 response = self._make_request(
788 conn,
789 method,
790 url,
791 timeout=timeout_obj,
792 body=body,
793 headers=headers,
794 chunked=chunked,
795 retries=retries,
796 response_conn=response_conn,
797 preload_content=preload_content,
798 decode_content=decode_content,
799 **response_kw,
800 )
802 # Everything went great!
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\urllib3\connectionpool.py:493, in HTTPConnectionPool._make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)
492 try:
--> 493 conn.request(
494 method,
495 url,
496 body=body,
497 headers=headers,
498 chunked=chunked,
499 preload_content=preload_content,
500 decode_content=decode_content,
501 enforce_content_length=enforce_content_length,
502 )
504 # We are swallowing BrokenPipeError (errno.EPIPE) since the server is
505 # legitimately able to close the connection after sending a valid response.
506 # With this behaviour, the received response is still readable.
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\urllib3\connection.py:500, in HTTPConnection.request(self, method, url, body, headers, chunked, preload_content, decode_content, enforce_content_length)
499 self.putheader(header, value)
--> 500 self.endheaders()
502 # If we're given a body we start sending that in chunks.
File C:\Program Files (x86)\Python3.13.12\Lib\http\client.py:1353, in HTTPConnection.endheaders(self, message_body, encode_chunked)
1352 raise CannotSendHeader()
-> 1353 self._send_output(message_body, encode_chunked=encode_chunked)
File C:\Program Files (x86)\Python3.13.12\Lib\http\client.py:1113, in HTTPConnection._send_output(self, message_body, encode_chunked)
1112 del self._buffer[:]
-> 1113 self.send(msg)
1115 if message_body is not None:
1116
1117 # create a consistent interface to message_body
File C:\Program Files (x86)\Python3.13.12\Lib\http\client.py:1057, in HTTPConnection.send(self, data)
1056 if self.auto_open:
-> 1057 self.connect()
1058 else:
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\urllib3\connection.py:331, in HTTPConnection.connect(self)
330 def connect(self) -> None:
--> 331 self.sock = self._new_conn()
332 if self._tunnel_host:
333 # If we're tunneling it means we're connected to our proxy.
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\urllib3\connection.py:211, in HTTPConnection._new_conn(self)
210 except socket.gaierror as e:
--> 211 raise NameResolutionError(self.host, self, e) from e
212 except SocketTimeout as e:
NameResolutionError: HTTPConnection(host='xxxx', port=80): Failed to resolve 'xxxx' ([Errno 11001] getaddrinfo failed)
The above exception was the direct cause of the following exception:
MaxRetryError Traceback (most recent call last)
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\requests\adapters.py:645, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
644 try:
--> 645 resp = conn.urlopen(
646 method=request.method,
647 url=url,
648 body=request.body,
649 headers=request.headers,
650 redirect=False,
651 assert_same_host=False,
652 preload_content=False,
653 decode_content=False,
654 retries=self.max_retries,
655 timeout=timeout,
656 chunked=chunked,
657 )
659 except (ProtocolError, OSError) as err:
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\urllib3\connectionpool.py:841, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
839 new_e = ProtocolError("Connection aborted.", new_e)
--> 841 retries = retries.increment(
842 method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]
843 )
844 retries.sleep()
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\urllib3\util\retry.py:535, in Retry.increment(self, method, url, response, error, _pool, _stacktrace)
534 reason = error or ResponseError(cause)
--> 535 raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
537 log.debug("Incremented Retry for (url='%s'): %r", url, new_retry)
MaxRetryError: HTTPConnectionPool(host='xxxx', port=80): Max retries exceeded with url: /CamWCFServices/QueryService.svc?wsdl (Caused by NameResolutionError("HTTPConnection(host='xxxx', port=80): Failed to resolve 'xxxx' ([Errno 11001] getaddrinfo failed)"))
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last)
Cell In[198], line 7
3 # 服务的 WSDL 地址(通常在服务地址后加 ?wsdl)
4 wsdl_url = 'http://xxxx/CamWCFServices/QueryService.svc?wsdl'
5
6 # 创建客户端
----> 7 client = Client(wsdl_url)
8
9 # 2. (强烈推荐) 打印出服务的完整结构,查看所有可用的方法名和参数
10 # 这一步会列出诸如 Login, Logout, ChangePassword 等具体方法
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\zeep\client.py:76, in Client.init(self, wsdl, wsse, transport, service_name, port_name, plugins, settings)
74 self.wsdl = wsdl
75 else:
---> 76 self.wsdl = Document(wsdl, self.transport, settings=self.settings)
77 self.wsse = wsse
78 self.plugins = plugins if plugins is not None else []
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\zeep\wsdl\wsdl.py:86, in Document.init(self, location, transport, base, settings)
77 self._definitions = (
78 {}
79 ) # type: typing.Dict[typing.Tuple[str, str], "Definition"]
80 self.types = Schema(
81 node=None,
82 transport=self.transport,
83 location=self.location,
84 settings=self.settings,
85 )
---> 86 self.load(location)
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\zeep\wsdl\wsdl.py:89, in Document.load(self, location)
88 def load(self, location):
---> 89 document = self._get_xml_document(location)
91 root_definitions = Definition(self, document, self.location)
92 root_definitions.resolve_imports()
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\zeep\wsdl\wsdl.py:149, in Document._get_xml_document(self, location)
141 def _get_xml_document(self, location: typing.IO) -> etree._Element:
142 """Load the XML content from the given location and return an
143 lxml.Element object.
144
(...) 147
148 """
--> 149 return load_external(
150 location, self.transport, self.location, settings=self.settings
151 )
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\zeep\loader.py:89, in load_external(url, transport, base_url, settings)
87 if base_url:
88 url = absolute_location(url, base_url)
---> 89 content = transport.load(url)
90 return parse_xml(content, transport, base_url, settings=settings)
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\zeep\transports.py:132, in Transport.load(self, url)
129 if response:
130 return bytes(response)
--> 132 content = self._load_remote_data(url)
134 if self.cache:
135 self.cache.add(url, content)
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\zeep\transports.py:144, in Transport._load_remote_data(self, url)
142 def _load_remote_data(self, url):
143 self.logger.debug("Loading remote data from: %s", url)
--> 144 response = self.session.get(url, timeout=self.load_timeout)
145 with closing(response):
146 response.raise_for_status()
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\requests\sessions.py:605, in Session.get(self, url, **kwargs)
597 r"""Sends a GET request. Returns :class:Response object.
598
599 :param url: URL for the new :class:Request object.
600 :param **kwargs: Optional arguments that request takes.
601 :rtype: requests.Response
602 """
604 kwargs.setdefault("allow_redirects", True)
--> 605 return self.request("GET", url, **kwargs)
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\requests\sessions.py:592, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
587 send_kwargs = {
588 "timeout": timeout,
589 "allow_redirects": allow_redirects,
590 }
591 send_kwargs.update(settings)
--> 592 resp = self.send(prep, **send_kwargs)
594 return resp
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\requests\sessions.py:706, in Session.send(self, request, **kwargs)
703 start = preferred_clock()
705 # Send the request
--> 706 r = adapter.send(request, **kwargs)
708 # Total elapsed time of the request (approximately)
709 elapsed = preferred_clock() - start
File C:\Program Files (x86)\Python3.13.12\Lib\site-packages\requests\adapters.py:678, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
674 if isinstance(e.reason, _SSLError):
675 # This branch is for urllib3 v1.22 and later.
676 raise SSLError(e, request=request)
--> 678 raise ConnectionError(e, request=request)
680 except ClosedPoolError as e:
681 raise ConnectionError(e, request=request)
ConnectionError: HTTPConnectionPool(host='xxxx', port=80): Max retries exceeded with url: /CamWCFServices/QueryService.svc?wsdl (Caused by NameResolutionError("HTTPConnection(host='xxxx', port=80): Failed to resolve 'xxxx' ([Errno 11001] getaddrinfo failed)"))
from zeep import Client
# 服务的 WSDL 地址(通常在服务地址后加 ?wsdl)
wsdl_url = 'http://xxxx/CamWCFServices/AuthenticationService.svc?wsdl'
# 创建客户端
client = Client(wsdl_url)
client.wsdl.dump()
#!/usr/bin/python3
import json
# Python 字典类型转换为 JSON 对象
data = {
'no' : 1,
'name' : 'W3CSchool',
'url' : 'http://www.w3cschool.cn'
}
json_str = json.dumps(data) # 对数据进行编码 json.loads(): 对数据进行解码。
print ("Python 原始数据:", repr(data))
print ("JSON 对象:", json_str)
Python 原始数据: {'no': 1, 'name': 'W3CSchool', 'url': 'http://www.w3cschool.cn'}
JSON 对象: {"no": 1, "name": "W3CSchool", "url": "http://www.w3cschool.cn"}
import pandas as pd
s = pd.Series()
print(s)
Series([], dtype: object)
import pandas as pd
import numpy as np
data = np.array([1.2323,3.4332,2.5452,0.3432])
s = pd.Series(data)
# s = pd.Series(data, index=[100,101,102,103])
# s=pd.Series( data, index, dtype, copy)
# data 输入的数据,可以是列表、常量、ndarray 数组等。
# index 索引值必须是惟一的,如果没有传递索引,则默认为 np.arrange(n)。
# dtype dtype表示数据类型,如果没有提供,则会自动判断得出。
# copy 表示对 data 进行拷贝,默认为 False。
print (s)
100 1.2323
101 3.4332
102 2.5452
103 0.3432
dtype: float64
# dict创建Series对象
import pandas as pd
import numpy as np
data = {'a':0.12, 'b':2.23, 'c':2.31}
s = pd.Series(data)
s = pd.Series(data,index=['b','c','d','a']) # 当传递的索引值无法找到与其对应的值时,使用 NaN(非数字)填充
print(s)
b 2.23
c 2.31
d NaN
a 0.12
dtype: float64
# 标量创建Series对象 , 如果 data 是标量值,则必须提供索引
import pandas as pd
import numpy as np
s = pd.Series(5, index =[0,1,2,3])
print(s)
0 5
1 5
2 5
3 5
dtype: int64
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print(s)
#print(s[0]) #位置下标
print(s['a']) #标签下标
a 1
b 2
c 3
d 4
e 5
dtype: int64
1
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print(s[:3])
a 1
b 2
c 3
dtype: int64
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print(s[-3:])
c 3
d 4
e 5
dtype: int64
import pandas as pd
s = pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])
print(s[['a','c','d']])
a 6
c 8
d 9
dtype: int64
import pandas as pd
s = pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])
try:
#不包含f值
print(s['f'])
except:
print('error')
error
import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print(s)
0 -1.301185
1 -1.573699
2 -0.534648
3 0.426099
4 -0.703329
dtype: float64
import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print ("The axes are:")
print(s.axes) # 以列表的形式返回所有行索引标签。
The axes are:
[RangeIndex(start=0, stop=5, step=1)]
import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print ("The dtype is:")
print(s.dtype)
The dtype is:
float64
import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print("是否为空对象?")
print (s.empty)
是否为空对象?
False
import pandas as pd
import numpy as np
# pd.DataFrame( data, index, columns, dtype, copy)
d =pd.DataFrame(np.random.randn(5),columns=['name'])
print(d)
name
0 -0.143610
1 -0.785086
2 -2.285494
3 0.092115
4 1.865269
import pandas as pd
data = [['Alex',10], ['Bob',12],['Clarke',13]]
d = pd.DataFrame(data, index=['rank1','rank2','rank3'],columns=['Name','Age'])
print(d)
Name Age
rank1 Alex 10
rank2 Bob 12
rank3 Clarke 13
import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data)
print(df)
Name Age
0 Tom 28
1 Jack 34
2 Steve 29
3 Ricky 42
import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print(df)
Name Age
rank1 Tom 28
rank2 Jack 34
rank3 Steve 29
rank4 Ricky 42
import pandas as pd
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
df = pd.DataFrame(data)
print(df)
a b c
0 1 2 NaN
1 5 10 20.0
import pandas as pd
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b'])
df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1'])
print(df1)
print(df2)
a b
first 1 2
second 5 10
a b1
first 1 NaN
second 5 NaN
import pandas as pd
# 传递一个字典形式的 Series,从而创建一个 DataFrame 对象,其输出结果的行索引是所有 index 的合集
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print(df)
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print(df ['one'])
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
#使用df['列']=值,插入新的数据列
df['three']=pd.Series([10,20,30],index=['a','b','c'])
print(df)
#将已经存在的数据列做相加运算
df['four']=df['one']+df['three']
print(df)
one two three
a 1.0 1 10.0
b 2.0 2 20.0
c 3.0 3 30.0
d NaN 4 NaN
one two three four
a 1.0 1 10.0 11.0
b 2.0 2 20.0 22.0
c 3.0 3 30.0 33.0
d NaN 4 NaN NaN
import pandas as pd
info=[['Jack',18],['Helen',19],['John',17]]
df=pd.DataFrame(info,columns=['name','age'])
print(df)
#注意是column参数
#数值1代表插入到columns列表的索引位置
df.insert(1,column='score',value=[91,90,75])
print(df)
name age
0 Jack 18
1 Helen 19
2 John 17
name score age
0 Jack 91 18
1 Helen 90 19
2 John 75 17
import pandas as pd
info=[['Jack',18],['Helen',19],['John',17]]
df=pd.DataFrame(info,columns=['name','age'])
print(df)
#注意是column参数
#数值1代表插入到columns列表的索引位置
df.insert(1,column='score',value=[91,90,75])
print(df)
name age
0 Jack 18
1 Helen 19
2 John 17
name score age
0 Jack 91 18
1 Helen 90 19
2 John 75 17
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
'three' : pd.Series([10,20,30], index=['a','b','c'])}
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
#使用del删除
del df['one']
print(df)
#使用pop方法删除
df.pop('two')
print(df)
Our dataframe is:
one two three
a 1.0 1 10.0
b 2.0 2 20.0
c 3.0 3 30.0
d NaN 4 NaN
two three
a 1 10.0
b 2 20.0
c 3 30.0
d 4 NaN
three
a 10.0
b 20.0
c 30.0
d NaN
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
print ("------------")
print(df.loc['b'])
Our dataframe is:
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
------------
one 2.0
two 2.0
Name: b, dtype: float64
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
print ("------------")
print(df.iloc[2])
Our dataframe is:
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
------------
one 3.0
two 3.0
Name: c, dtype: float64
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
print ("------------")
#左闭右开
print(df[2:4])
Our dataframe is:
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
------------
one two
c 3.0 3
d NaN 4
import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])
# 在行末追加新数据行
df = pd.concat([df, df2], ignore_index=True)
print(df)
a b
0 1 2
1 3 4
2 5 6
3 7 8
import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])
# 在行末追加新数据行
df = pd.concat([df, df2], ignore_index=True)
print(df)
# 注意此处调用了drop()方法
# 删除数据行
df = df.drop(0)
print(df)
a b
0 1 2
1 3 4
2 5 6
3 7 8
a b
1 3 4
2 5 6
3 7 8
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['W3Cschool','编程狮',"百度",'360搜索','谷歌','微学苑','Bing搜索']),
'years':pd.Series([5,6,15,28,3,19,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#构建DataFrame
df = pd.DataFrame(d)
#输出series
print(df)
print ("After T:")
print(df.T)
Name years Rating
0 W3Cschool 5 4.23
1 编程狮 6 3.24
2 百度 15 3.98
3 360搜索 28 2.56
4 谷歌 3 3.20
5 微学苑 19 4.60
6 Bing搜索 23 3.80
After T:
0 1 2 3 4 5 6
Name W3Cschool 编程狮 百度 360搜索 谷歌 微学苑 Bing搜索
years 5 6 15 28 3 19 23
Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['W3Cschool','编程狮',"百度",'360搜索','谷歌','微学苑','Bing搜索']),
'years':pd.Series([5,6,15,28,3,19,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#构建DataFrame
df = pd.DataFrame(d)
#输出行、列标签
print(df.axes)
[RangeIndex(start=0, stop=7, step=1), Index(['Name', 'years', 'Rating'], dtype='str')]
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['W3Cschool','编程狮',"百度",'360搜索','谷歌','微学苑','Bing搜索']),
'years':pd.Series([5,6,15,28,3,19,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#构建DataFrame
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
print ("------------")
#输出行、列标签
print(df.dtypes)
Our dataframe is:
Name years Rating
0 W3Cschool 5 4.23
1 编程狮 6 3.24
2 百度 15 3.98
3 360搜索 28 2.56
4 谷歌 3 3.20
5 微学苑 19 4.60
6 Bing搜索 23 3.80
------------
Name str
years int64
Rating float64
dtype: object
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['W3Cschool','编程狮',"百度",'360搜索','谷歌','微学苑','Bing搜索']),
'years':pd.Series([5,6,15,28,3,19,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#构建DataFrame
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
#DataFrame的维度
print(df.ndim)
Our dataframe is:
Name years Rating
0 W3Cschool 5 4.23
1 编程狮 6 3.24
2 百度 15 3.98
3 360搜索 28 2.56
4 谷歌 3 3.20
5 微学苑 19 4.60
6 Bing搜索 23 3.80
2
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['W3Cschool','编程狮',"百度",'360搜索','谷歌','微学苑','Bing搜索']),
'years':pd.Series([5,6,15,28,3,19,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#构建DataFrame
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
#DataFrame的形状
print(df.shape)
Our dataframe is:
Name years Rating
0 W3Cschool 5 4.23
1 编程狮 6 3.24
2 百度 15 3.98
3 360搜索 28 2.56
4 谷歌 3 3.20
5 微学苑 19 4.60
6 Bing搜索 23 3.80
(7, 3)
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['W3Cschool','编程狮',"百度",'360搜索','谷歌','微学苑','Bing搜索']),
'years':pd.Series([5,6,15,28,3,19,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#构建DataFrame
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
#DataFrame的中元素个数
print(df.size)
Our dataframe is:
Name years Rating
0 W3Cschool 5 4.23
1 编程狮 6 3.24
2 百度 15 3.98
3 360搜索 28 2.56
4 谷歌 3 3.20
5 微学苑 19 4.60
6 Bing搜索 23 NaN
21
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['W3Cschool','编程狮',"百度",'360搜索','谷歌','微学苑','Bing搜索']),
'years':pd.Series([5,6,15,28,3,19,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#构建DataFrame
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
#DataFrame的数据
print(df.values)
Our dataframe is:
Name years Rating
0 W3Cschool 5 4.23
1 编程狮 6 3.24
2 百度 15 3.98
3 360搜索 28 2.56
4 谷歌 3 3.20
5 微学苑 19 4.60
6 Bing搜索 23 3.80
[['W3Cschool' 5 4.23]
['编程狮' 6 3.24]
['百度' 15 3.98]
['360搜索' 28 2.56]
['谷歌' 3 3.2]
['微学苑' 19 4.6]
['Bing搜索' 23 3.8]]
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['W3CSchool','编程狮',"百度",'360搜索','谷歌','微学苑','Bing搜索']),
'years':pd.Series([5,6,15,28,3,19,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#构建DataFrame
df = pd.DataFrame(d)
#获取前3行数据
print(df.head(3))
Name years Rating
0 W3CSchool 5 4.23
1 编程狮 6 3.24
2 百度 15 3.98
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['W3CSchool','编程狮',"百度",'360搜索','谷歌','微学苑','Bing搜索']),
'years':pd.Series([5,6,15,28,3,19,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#构建DataFrame
df = pd.DataFrame(d)
#获取后2行数据
print(df.tail(2))
Name years Rating
5 微学苑 19 4.6
6 Bing搜索 23 3.8
# DataFrame.shift(periods=1, freq=None, axis=0)
# periods 类型为int,表示移动的幅度,可以是正数,也可以是负数,默认值为1。
# freq 日期偏移量,默认值为None,适用于时间序。取值为符合时间规则的字符串。
# axis 如果是 0 或者 "index" 表示上下移动,如果是 1 或者 "columns" 则会左右移动。
# fill_value 该参数用来填充缺失值。
import pandas as pd
info = pd.DataFrame({'a_data':[40,28,39,32,18], 'b_data':[20,37,41,35,45],'c_data':[22,17,11,25,15]})
print ("Our dataframe is:")
print(info)
#移动幅度
info.shift(periods=3)
Our dataframe is:
a_data b_data c_data
0 40 20 22
1 28 37 17
2 39 41 11
3 32 35 25
4 18 45 15
| a_data | b_data | c_data | |
|---|---|---|---|
| 0 | NaN | NaN | NaN |
| 1 | NaN | NaN | NaN |
| 2 | NaN | NaN | NaN |
| 3 | 40.0 | 20.0 | 22.0 |
| 4 | 28.0 | 37.0 | 17.0 |
import pandas as pd
info= pd.DataFrame({'a_data': [40, 28, 39, 32, 18],
'b_data': [20, 37, 41, 35, 45],
'c_data': [22, 17, 11, 25, 15]})
#移动幅度为3
print(info.shift(periods=3))
print(info.shift(periods=3,fill_value= 52))
#将缺失值和原数值替换为52
info.shift(periods=3,axis=1,fill_value= 52)
a_data b_data c_data
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 40.0 20.0 22.0
4 28.0 37.0 17.0
a_data b_data c_data
0 52 52 52
1 52 52 52
2 52 52 52
3 40 20 22
4 28 37 17
import pandas as pd
import numpy as np
#创建字典型series结构
d = {'Name':pd.Series(['小明','小亮','小红','小华','老赵','小曹','小陈',
'老李','老王','小冯','小何','老张']),
'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])
}
df = pd.DataFrame(d)
print(df)
# 只对数值列求和
# print(df.sum(axis=1)) TypeError: can only concatenate str (not "int") to str
print(df[['Age', 'Rating']].sum(axis=1))
Name Age Rating
0 小明 25 4.23
1 小亮 26 3.24
2 小红 25 3.98
3 小华 23 2.56
4 老赵 30 3.20
5 小曹 29 4.60
6 小陈 23 3.80
7 老李 34 3.78
8 老王 40 2.98
9 小冯 30 4.80
10 小何 51 4.10
11 老张 46 3.65
0 29.23
1 29.24
2 28.98
3 25.56
4 33.20
5 33.60
6 26.80
7 37.78
8 42.98
9 34.80
10 55.10
11 49.65
dtype: float64
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['小明','小亮','小红','小华','老赵','小曹','小陈',
'老李','老王','小冯','小何','老张']),
'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])
}
df = pd.DataFrame(d)
print(df[['Age', 'Rating']].mean(axis=1)) # 水平方向求平均值
print(df[['Age', 'Rating']].sum(axis=0)) # 水平方向求平均值
0 14.615
1 14.620
2 14.490
3 12.780
4 16.600
5 16.800
6 13.400
7 18.890
8 21.490
9 17.400
10 27.550
11 24.825
dtype: float64
Age 382.00
Rating 44.92
dtype: float64
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['小明','小亮','小红','小华','老赵','小曹','小陈',
'老李','老王','小冯','小何','老张']),
'Age':pd.Series([25,26,25,23,59,19,23,44,40,30,51,54]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])
}
df = pd.DataFrame(d)
print(df[['Age','Rating']].std()) #求标准差
# 标准差是方差的算术平方根,它能反映一个数据集的离散程度。注意,平均数相同的两组数据,标准差未必相同。
Age 13.976983
Rating 0.661628
dtype: float64
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['小明','小亮','小红','小华','老赵','小曹','小陈',
'老李','老王','小冯','小何','老张']),
'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])
}
#创建DataFrame对象
df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)
print('-------------1')
print(df[['Age','Rating']].cumsum(axis=0))
print('-------------2')
#求出数据的所有描述信息
print(df.describe())
# count() 统计某个非空值的数量。
# sum() 求和
# mean() 求均值
# median() 求中位数
# mode() 求众数
# std() 求标准差
# min() 求最小值
# max() 求最大值
# abs() 求绝对值
# prod() 求所有数值的乘积。
# cumsum() 计算累计和,axis=0,按照行累加 (行方向);axis=1 (列方向),按照列累加。
# cumprod() 计算累计积,axis=0,按照行累积;axis=1,按照列累积。
# corr() 计算数列或变量之间的相关系数,取值-1到1,值越大表示关联性越强。
Our dataframe is:
Name Age Rating
0 小明 25 4.23
1 小亮 26 3.24
2 小红 25 3.98
3 小华 23 2.56
4 老赵 30 3.20
5 小曹 29 4.60
6 小陈 23 3.80
7 老李 34 3.78
8 老王 40 2.98
9 小冯 30 4.80
10 小何 51 4.10
11 老张 46 3.65
-------------1
Age Rating
0 25 4.23
1 51 7.47
2 76 11.45
3 99 14.01
4 129 17.21
5 158 21.81
6 181 25.61
7 215 29.39
8 255 32.37
9 285 37.17
10 336 41.27
11 382 44.92
-------------2
Age Rating
count 12.000000 12.000000
mean 31.833333 3.743333
std 9.232682 0.661628
min 23.000000 2.560000
25% 25.000000 3.230000
50% 29.500000 3.790000
75% 35.500000 4.132500
max 51.000000 4.800000
import pandas as pd
import numpy as np
d = {'Name':pd.Series(['小明','小亮','小红','小华','老赵','小曹','小陈',
'老李','老王','小冯','小何','老张']),
'Age':pd.Series([25,26,25,23,59,19,23,44,40,30,51,54]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])
}
df = pd.DataFrame(d)
print(df.describe(include="all"))
Name Age Rating
count 12 12.000000 12.000000
unique 12 NaN NaN
top 小明 NaN NaN
freq 1 NaN NaN
mean NaN 34.916667 3.743333
std NaN 13.976983 0.661628
min NaN 19.000000 2.560000
25% NaN 24.500000 3.230000
50% NaN 28.000000 3.790000
75% NaN 45.750000 4.132500
max NaN 59.000000 4.800000
import pandas as pd
import numpy as np
#自定义函数
def adder(ele1,ele2):
return ele1+ele2
#操作DataFrame
df = pd.DataFrame(np.random.randn(4,3),columns=['c1','c2','c3'])
#相加前
print ("相加前,Our dataframe is:")
print(df)
#相加后
print ("相加后,Our dataframe is:")
print(df.pipe(adder,3)) #传入自定义函数以及要相加的数值3
#1) 操作整个 DataFrame 的函数:pipe()
#2) 操作行或者列的函数:apply()
#3) 操作单一元素的函数:applymap()
相加前,Our dataframe is:
c1 c2 c3
0 0.038294 0.815040 1.439826
1 -0.832208 2.135313 -1.188166
2 1.277932 2.334962 -1.458089
3 0.922774 0.768959 -1.487218
相加后,Our dataframe is:
c1 c2 c3
0 3.038294 3.815040 4.439826
1 2.167792 5.135313 1.811834
2 4.277932 5.334962 1.541911
3 3.922774 3.768959 1.512782
# 操作行或者列的函数:apply()
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
#相加前
print ("相加前,Our dataframe is:")
print(df)
df.apply(np.mean)
#默认按列操作,计算每一列均值
print(df.apply(np.mean,axis=1))
#1) 操作整个 DataFrame 的函数:pipe()
#2) 操作行或者列的函数:apply()
#3) 操作单一元素的函数:applymap()
相加前,Our dataframe is:
col1 col2 col3
0 -0.204082 -1.048920 0.676485
1 1.404126 1.212578 0.819025
2 0.915005 -0.071107 -1.167079
3 -0.096724 -0.904914 0.232051
4 0.105259 1.138378 -1.033386
0 -0.192172
1 1.145243
2 -0.107727
3 -0.256529
4 0.070083
dtype: float64
import pandas as pd
import numpy as np
# np.random.randn(5,3) 的意思是:生成一个 5行 × 3列 的随机数组,数组中的数值服从标准正态分布
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print ("Our dataframe is:")
print(df)
print(df.apply(lambda x: x.max() - x.min()))
Our dataframe is:
col1 col2 col3
0 -0.703362 0.490160 -0.736814
1 -1.831531 1.436093 1.205781
2 1.296627 0.525122 3.700465
3 -1.375367 1.966999 -0.372745
4 -0.075954 1.162118 1.555802
col1 3.128158
col2 1.476839
col3 4.437279
dtype: float64
# 操作单一函数
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print ("Our dataframe is:")
print(df)
#自定义函数lambda函数
print(df['col1'].map(lambda x:x*100))
Our dataframe is:
col1 col2 col3
0 -0.166415 1.054307 0.903546
1 0.389020 -0.497688 1.695553
2 0.738491 0.630426 0.501659
3 0.080688 0.850576 0.419460
4 0.311341 -1.718696 0.306189
0 -16.641493
1 38.901998
2 73.849120
3 8.068782
4 31.134113
Name: col1, dtype: float64
import pandas as pd
import numpy as np
#自定义函数
# np.random.randn(5,3) 的意思是:生成一个 5行 × 3列 的随机数组,数组中的数值服从标准正态分布
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print ("Our dataframe is:")
print(df)
#print(df.applymap(lambda x:x*10))
print(df.apply(np.mean))
Our dataframe is:
col1 col2 col3
0 0.111216 1.250359 -0.536152
1 -0.005122 0.525063 -1.015971
2 2.035827 -0.725858 -0.097283
3 -1.460743 0.287439 -0.547900
4 -0.864946 -2.668383 -1.387242
col1 -0.036754
col2 -0.266276
col3 -0.716910
dtype: float64
import pandas as pd
import numpy as np
N=20
df = pd.DataFrame({
'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),
'x': np.linspace(0,stop=N-1,num=N),
'y': np.random.rand(N),
'C': np.random.choice(['Low','Medium','High'],N).tolist(),
'D': np.random.normal(100, 10, size=(N)).tolist()
})
# 总结:reindex() 的核心价值在于灵活的数据对齐和重组,特别适合处理不完整数据、时间序列标准化、以及多数据源整合的场景。
# df = pd.DataFrame({
# 'A': pd.date_range(start='2016-01-01',periods=N,freq='D'), # 生成从 2016-01-01 开始的连续日期序列,periods=N:生成20个日期,freq='D':频率为每天(Day)
# 'x': np.linspace(0,stop=N-1,num=N), # linspace = linear space(线性空间)在 [0, 19] 区间内均匀生成20个数字,num=N:生成20个点
# 'y': np.random.rand(N), # rand(N):# 生成N个 [0, 1) 区间内的随机数
# 'C': np.random.choice(['Low','Medium','High'],N).tolist(), #choice():从给定列表中随机选择,['Low','Medium','High']:可选类别 N:选择20次.tolist():转换为Python列表,结果:20个随机选择的类别值
# 'D': np.random.normal(100, 10, size=(N)).tolist() # normal(100, 10, size=N):正态分布 ,均值(mean)= 100,标准差(std)= 10,标准差(std)= 10,.tolist():转换为列表,结果:20个均值为100、标准差为10的随机数
# })
print ("Our dataframe is:")
print(df)
#重置行、列索引标签
df_reindexed = df.reindex(index=[0,2,5], columns=['A', 'C', 'x']) #行筛选:只保留原DataFrame中索引为 0、2、5 的行 ,列重排:列按 A → C → B 的顺序排列 , 'B' 列不存在,所以会创建新列并填充 NaN
print(df_reindexed)
# 根据新的索引标签重新组织数据
# 选择特定行并重新排序
df_reindexed = df.reindex(index=[5, 0, 2]) # 按指定顺序提取行
print(df_reindexed)
Our dataframe is:
A x y C D
0 2016-01-01 0.0 0.856003 High 107.436086
1 2016-01-02 1.0 0.520230 Medium 100.618346
2 2016-01-03 2.0 0.324962 High 96.863845
3 2016-01-04 3.0 0.667463 Low 108.183946
4 2016-01-05 4.0 0.219549 High 94.836469
5 2016-01-06 5.0 0.589637 Medium 103.651234
6 2016-01-07 6.0 0.993299 Low 92.310995
7 2016-01-08 7.0 0.552814 High 86.048915
8 2016-01-09 8.0 0.888580 Medium 97.569122
9 2016-01-10 9.0 0.982069 High 97.142976
10 2016-01-11 10.0 0.830956 High 83.607825
11 2016-01-12 11.0 0.562639 Medium 91.576969
12 2016-01-13 12.0 0.376234 Medium 94.495539
13 2016-01-14 13.0 0.196831 High 112.721500
14 2016-01-15 14.0 0.147930 High 100.581221
15 2016-01-16 15.0 0.045711 Medium 96.421188
16 2016-01-17 16.0 0.561389 Low 99.322015
17 2016-01-18 17.0 0.470437 High 88.108835
18 2016-01-19 18.0 0.025993 High 100.344257
19 2016-01-20 19.0 0.717828 Low 83.746130
A C x
0 2016-01-01 High 0.0
2 2016-01-03 High 2.0
5 2016-01-06 Medium 5.0
A x y C D
5 2016-01-06 5.0 0.589637 Medium 103.651234
0 2016-01-01 0.0 0.856003 High 107.436086
2 2016-01-03 2.0 0.324962 High 96.863845
# 创建每日数据
daily_data = pd.DataFrame({
'value': np.random.randn(10)
}, index=pd.date_range('2024-01-01', periods=10, freq='D'))
# 重采样到每周,但保留每周三的索引
weekly_index = pd.date_range('2024-01-03', periods=5, freq='W-WED')
weekly_aligned = daily_data.reindex(weekly_index)
print(weekly_aligned)
value
2024-01-03 -1.525569
2024-01-10 0.680518
2024-01-17 NaN
2024-01-24 NaN
2024-01-31 NaN
import pandas as pd
import numpy as np
# np.random.randn(6,3) 的意思是:生成一个 6行 × 3列 的随机数组,数组中的数值服从标准正态分布
df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
print (df1)
#对行和列重新命名
print (df1.rename(columns={'col1' : 'c1', 'col2' : 'c2'},index = {0 : 'apple', 1 : 'banana', 2 : 'durian'}))
col1 col2 col3
0 0.510038 0.657313 0.419436
1 0.968435 2.300379 0.005950
2 -2.128789 -1.642617 1.037803
3 -1.852611 0.618077 0.642210
4 0.920614 0.080171 -0.273413
5 0.565044 -1.262588 -0.209325
c1 c2 col3
apple 0.510038 0.657313 0.419436
banana 0.968435 2.300379 0.005950
durian -2.128789 -1.642617 1.037803
3 -1.852611 0.618077 0.642210
4 0.920614 0.080171 -0.273413
5 0.565044 -1.262588 -0.209325
# 如何遍历 Series 和 DataFrame 结构呢?我们应该明确,它们的数据结构类型不同的,遍历的方法必然会存在差异。
# 对于 Series 而言,您可以把它当做一维数组进行遍历操作;而像 DataFrame 这种二维数据表结构,则类似于遍历 Python 字典。
import pandas as pd
import numpy as np
N=20
df = pd.DataFrame({
'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),
'x': np.linspace(0,stop=N-1,num=N),
'y': np.random.rand(N),
'C': np.random.choice(['Low','Medium','High'],N).tolist(),
'D': np.random.normal(100, 10, size=(N)).tolist()
})
print(df)
for col in df:
print (col)
A x y C D
0 2016-01-01 0.0 0.154425 Low 91.810349
1 2016-01-02 1.0 0.420169 Medium 96.869672
2 2016-01-03 2.0 0.224631 Medium 87.838601
3 2016-01-04 3.0 0.779524 High 94.985176
4 2016-01-05 4.0 0.141910 High 91.650999
5 2016-01-06 5.0 0.882983 Medium 100.974930
6 2016-01-07 6.0 0.219973 Medium 107.067067
7 2016-01-08 7.0 0.537370 Medium 92.631576
8 2016-01-09 8.0 0.622410 High 91.421905
9 2016-01-10 9.0 0.672639 Low 88.076897
10 2016-01-11 10.0 0.749907 Low 96.481483
11 2016-01-12 11.0 0.913485 Low 94.075682
12 2016-01-13 12.0 0.821394 Medium 101.120607
13 2016-01-14 13.0 0.778466 High 112.079977
14 2016-01-15 14.0 0.146298 Low 103.235315
15 2016-01-16 15.0 0.465927 Medium 95.615634
16 2016-01-17 16.0 0.993642 Medium 92.715859
17 2016-01-18 17.0 0.573827 High 85.491257
18 2016-01-19 18.0 0.246584 Low 101.384886
19 2016-01-20 19.0 0.954440 Medium 119.866424
A
x
y
C
D
#如果想要遍历 DataFrame 的每一行,我们下列函数:
#1) iteritems():以键值对 (key,value) 的形式遍历;
#2) iterrows():以 (row_index,row) 的形式遍历行;
#3) itertuples():使用已命名元组的方式对行遍历。
import pandas as pd
import numpy as np
# np.random.randn(4,3) 的意思是:生成一个 4行 × 3列 的随机数组,数组中的数值服从标准正态分布
df = pd.DataFrame(np.random.randn(4,3),columns=['col1','col2','col3'])
print ("Our dataframe is:")
print(df)
# 兼容新旧版本
if hasattr(df, 'items'):
iterator = df.items()
else:
iterator = df.iteritems()
for key,value in iterator: # 在较新的 pandas 版本(≥2.0)中,iteritems() 方法已被弃用并移除。现在应该使用 items() 方法。
print (key,value)
Our dataframe is:
col1 col2 col3
0 0.159360 1.172389 -1.143210
1 1.372073 -0.783522 0.293872
2 -1.594622 -0.212000 0.297785
3 1.116387 -1.344542 -1.222366
col1 0 0.159360
1 1.372073
2 -1.594622
3 1.116387
Name: col1, dtype: float64
col2 0 1.172389
1 -0.783522
2 -0.212000
3 -1.344542
Name: col2, dtype: float64
col3 0 -1.143210
1 0.293872
2 0.297785
3 -1.222366
Name: col3, dtype: float64
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(3,3),columns = ['col1','col2','col3'])
print(df)
for row_index,row in df.iterrows():
print (row_index,row)
#2) iterrows():以 (row_index,row) 的形式遍历行;
col1 col2 col3
0 2.402866 0.077434 1.894128
1 1.343899 -1.049230 0.986463
2 0.334386 1.852667 -1.156831
0 col1 2.402866
col2 0.077434
col3 1.894128
Name: 0, dtype: float64
1 col1 1.343899
col2 -1.049230
col3 0.986463
Name: 1, dtype: float64
2 col1 0.334386
col2 1.852667
col3 -1.156831
Name: 2, dtype: float64
# itertuples() 同样将返回一个迭代器,该方法会把 DataFrame 的每一行生成一个元组,示例如下:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(3,3),columns = ['c1','c2','c3'])
print ("Our dataframe is:")
print(df)
for row in df.itertuples():
print(row)
Our dataframe is:
c1 c2 c3
0 0.755590 0.414409 0.726648
1 0.745596 0.678002 0.962584
2 0.337560 0.008657 0.278445
Pandas(Index=0, c1=0.7555897444168457, c2=0.41440935896958764, c3=0.7266480097666624)
Pandas(Index=1, c1=0.745595650796805, c2=0.6780023919244149, c3=0.962583990520094)
Pandas(Index=2, c1=0.33756029005211274, c2=0.008657234377282386, c3=0.2784447867049218)
# 迭代器返回的是原对象的副本,所以,如果在迭代过程中修改元素值,不会影响原对象,这一点需要大家注意。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(3,3),columns = ['col1','col2','col3'])
print ("Our dataframe is:")
print(df)
for index, row in df.iterrows():
row['a'] = 15
print (df)
Our dataframe is:
col1 col2 col3
0 2.019013 0.638640 -0.755296
1 -0.583502 -1.239224 -1.541913
2 2.019092 -0.575949 2.104049
col1 col2 col3
0 2.019013 0.638640 -0.755296
1 -0.583502 -1.239224 -1.541913
2 2.019092 -0.575949 2.104049
# Pands 提供了两种排序方法,分别是按标签排序和按数值排序
import pandas as pd
import numpy as np
#行标签乱序排列,列标签乱序排列
# np.random.randn(10,2) 的意思是:生成一个 10行 × 2列 的随机数组,数组中的数值服从标准正态分布
unsorted_df=pd.DataFrame(np.random.randn(10,2),index=[1,6,4,2,3,5,9,8,0,7],columns=['col2','col1'])
print(unsorted_df)
col2 col1
1 -0.856553 -0.051371
6 0.143209 0.114808
4 -1.616748 -0.529336
2 0.380091 0.008824
3 -0.530838 2.651459
5 -0.614479 -1.302501
9 0.299513 -0.395020
8 0.131043 -1.147272
0 -0.596713 -0.017270
7 0.565021 0.762364
# 使用 sort_index() 方法对行标签排序,指定轴参数(axis)或者排序顺序。或者可以对 DataFrame 进行排序。默认情况下,按照行标签序排序。
import pandas as pd
import numpy as np
# np.random.randn(10,2) 的意思是:生成一个 10行 × 2列 的随机数组,数组中的数值服从标准正态分布
unsorted_df = pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7],columns = ['col2','col1'])
sorted_df=unsorted_df.sort_index()
print(sorted_df)
col2 col1
0 0.479170 -0.133887
1 -0.323890 0.399695
2 0.960522 1.741738
3 -1.881636 1.435012
4 -0.497655 -0.609340
5 -1.101140 0.887388
6 0.099208 2.166321
7 0.563407 1.261356
8 0.397925 -0.116910
9 0.555108 0.265515
# 通过将布尔值传递给ascending参数,可以控制排序的顺序(行号顺序)
import pandas as pd
import numpy as np
unsorted_df = pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7],columns = ['col2','col1'])
sorted_df = unsorted_df.sort_index(ascending=False)
print(sorted_df)
col2 col1
9 1.320324 0.482752
8 -0.384288 2.005691
7 1.593703 0.631832
6 0.051639 -0.889286
5 -1.627168 -0.211868
4 -0.114722 0.487896
3 0.258149 0.209472
2 1.062540 1.056115
1 -1.239550 2.320876
0 -1.183959 -0.489089
# 通过给 axis 轴参数传递 0 或 1,可以对列标签进行排序。默认情况下,axis=0 表示按行排序;而 axis=1 则表示按列排序。
import pandas as pd
import numpy as np
unsorted_df = pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7],columns = ['col2','col1'])
print ("Our dataframe is:")
print(unsorted_df)
sorted_df=unsorted_df.sort_index(axis=1,ascending=True)
print (sorted_df)
Our dataframe is:
col2 col1
1 -1.619916 0.659058
4 -0.791608 -0.560728
6 -1.303180 0.191199
2 0.618038 0.324863
3 -2.135380 -0.602437
5 1.244863 -0.133559
9 -0.524421 0.705501
8 -0.004661 -1.728656
0 0.075790 0.775176
7 -1.759934 1.316516
col1 col2
1 0.659058 -1.619916
4 -0.560728 -0.791608
6 0.191199 -1.303180
2 0.324863 0.618038
3 -0.602437 -2.135380
5 -0.133559 1.244863
9 0.705501 -0.524421
8 -1.728656 -0.004661
0 0.775176 0.075790
7 1.316516 -1.759934
# 与标签排序类似,sort_values() 表示按值排序。它接受一个by参数,该参数值是要排序数列的 DataFrame 列名
import pandas as pd
import numpy as np
unsorted_df = pd.DataFrame({'col1':[2,3,4,1],'col2':[1,3,2,4]})
print ("Our dataframe is:")
print(unsorted_df)
sorted_df = unsorted_df.sort_values(by='col1')
print (sorted_df)
Our dataframe is:
col1 col2
0 2 1
1 3 3
2 4 2
3 1 4
col1 col2
3 1 4
0 2 1
1 3 3
2 4 2
# 注意:当对 col1 列排序时,相应的 col2 列的元素值和行索引也会随 col1 一起改变。by 参数可以接受一个列表参数值
import pandas as pd
import numpy as np
unsorted_df = pd.DataFrame({'col1':[2,3,4,1],'col2':[1,3,2,4]})
print ("Our dataframe is:")
print(unsorted_df)
sorted_df = unsorted_df.sort_values(by=['col1','col2'])
print (sorted_df)
Our dataframe is:
col1 col2
0 2 1
1 3 3
2 4 2
3 1 4
col1 col2
3 1 4
0 2 1
1 3 3
2 4 2
# sort_values() 提供了参数kind用来指定排序算法。这里有三种排序算法:
# mergesort
# heapsort
# quicksort
# 默认为 quicksort(快速排序) ,其中 Mergesort 归并排序是最稳定的算法
import pandas as pd
import numpy as np
unsorted_df = pd.DataFrame({'col1':[2,3,4,1],'col2':[1,3,2,4]})
sorted_df = unsorted_df.sort_values(by='col1' ,kind='mergesort')
print (sorted_df)
col1 col2
3 1 4
0 2 1
1 3 3
2 4 2
# 数据去重复
# drop_duplicates()函数的语法格式如下: df.drop_duplicates(subset=['A','B','C'],keep='first',inplace=True)
# subset:表示要进去重的列名,默认为 None。
# keep:有三个可选参数,分别是 first、last、False,默认为 first,表示只保留第一次出现的重复项,删除其余重复项,last 表示只保留最后一次出现的重复项,False 则表示删除所有重复项。
# inplace:布尔值参数,默认为 False 表示删除重复项后返回一个副本,若为 Ture 则表示直接在原数据上删除重复项。
import pandas as pd
data={
'A':[1,0,1,1],
'B':[0,2,5,0],
'C':[4,0,4,4],
'D':[1,0,1,1]
}
df=pd.DataFrame(data=data)
print ("Our dataframe is:")
print(df)
#默认保留第一次出现的重复项
df.drop_duplicates()
Our dataframe is:
A B C D
0 1 0 4 1
1 0 2 0 0
2 1 5 4 1
3 1 0 4 1
| A | B | C | D | |
|---|---|---|---|---|
| 0 | 1 | 0 | 4 | 1 |
| 1 | 0 | 2 | 0 | 0 |
| 2 | 1 | 5 | 4 | 1 |
import pandas as pd
data={
'A':[1,0,1,1],
'B':[0,2,5,0],
'C':[4,0,4,4],
'D':[1,0,1,1]
}
df=pd.DataFrame(data=data)
print ("Our dataframe is:")
print(df)
#默认保留第一次出现的重复项
df.drop_duplicates(keep=False) # keep=False删除所有重复项
print(df)
Our dataframe is:
A B C D
0 1 0 4 1
1 0 2 0 0
2 1 5 4 1
3 1 0 4 1
A B C D
0 1 0 4 1
1 0 2 0 0
2 1 5 4 1
3 1 0 4 1
import pandas as pd
data={
'A':[1,3,3,3],
'B':[0,1,2,0],
'C':[4,5,4,4],
'D':[3,3,3,3]
}
df=pd.DataFrame(data=data)
print ("Our dataframe is:")
print(df)
#去除所有重复项,对于B列来说两个0是重复项
df2 = df.drop_duplicates(subset=['B'],keep=False)
print(df2)
#简写,省去subset参数
df3 = df.drop_duplicates(['B'],keep=False)
print(df3)
Our dataframe is:
A B C D
0 1 0 4 3
1 3 1 5 3
2 3 2 4 3
3 3 0 4 3
A B C D
1 3 1 5 3
2 3 2 4 3
A B C D
1 3 1 5 3
2 3 2 4 3
import pandas as pd
data={
'A':[1,3,3,3],
'B':[0,1,2,0],
'C':[4,5,4,4],
'D':[3,3,3,3]
}
df=pd.DataFrame(data=data)
print ("Our dataframe is:")
print(df)
#去除所有重复项,对于B来说两个0是重复项
df2 = df.drop_duplicates(subset=['B'],keep=False)
print(df2)
#重置索引,从0重新开始
df3 = df.reset_index(drop=True)
print(df3)
Our dataframe is:
A B C D
0 1 0 4 3
1 3 1 5 3
2 3 2 4 3
3 3 0 4 3
A B C D
1 3 1 5 3
2 3 2 4 3
A B C D
0 1 0 4 3
1 3 1 5 3
2 3 2 4 3
3 3 0 4 3
# 指定多列同时去重复
import numpy as np
import pandas as pd
df = pd.DataFrame({'Country ID':[1,1,2,12,34,23,45,34,23,12,2,3,4,1],
'Age':[12,12,15,18, 19, 25, 21, 25, 25, 18, 25,12,32,18],
'Group ID':['a','z','c','a','b','s','d','a','b','s','a','d','a','f']})
print ("Our dataframe is:")
print(df)
#last只保留最后一个重复项
df2 = df.drop_duplicates(['Age','Group ID'],keep='last')
print(df2)
Our dataframe is:
Country ID Age Group ID
0 1 12 a
1 1 12 z
2 2 15 c
3 12 18 a
4 34 19 b
5 23 25 s
6 45 21 d
7 34 25 a
8 23 25 b
9 12 18 s
10 2 25 a
11 3 12 d
12 4 32 a
13 1 18 f
Country ID Age Group ID
0 1 12 a
1 1 12 z
2 2 15 c
3 12 18 a
4 34 19 b
5 23 25 s
6 45 21 d
8 23 25 b
9 12 18 s
10 2 25 a
11 3 12 d
12 4 32 a
13 1 18 f
import pandas as pd
import numpy as np
s = pd.Series(['C', 'Python', 'java', 'GO', np.nan, '1125','javascript'])
print(s.str.lower())
0 c
1 python
2 java
3 go
4 NaN
5 1125
6 javascript
dtype: str
import pandas as pd
import numpy as np
s = pd.Series(['C', 'Python', 'java', 'go', np.nan, '1125','javascript'])
print ("Our Serie is:")
print(s)
print(s.str.len())
Our Serie is:
0 C
1 Python
2 java
3 go
4 NaN
5 1125
6 javascript
dtype: str
0 1.0
1 6.0
2 4.0
3 2.0
4 NaN
5 4.0
6 10.0
dtype: float64
import pandas as pd
import numpy as np
s = pd.Series(['C ', ' Python', 'java', 'go', np.nan, '1125 ','javascript'])
print ("Our Serie is:")
print(s)
# 去除字符串两边的空格(包含换行符)。
print(s.str.strip())
Our Serie is:
0 C
1 Python
2 java
3 go
4 NaN
5 1125
6 javascript
dtype: str
0 C
1 Python
2 java
3 go
4 NaN
5 1125
6 javascript
dtype: str
import pandas as pd
import numpy as np
s = pd.Series(['C ',' Python','java','go','1125 ','javascript'])
print ("Our Serie is:")
print(s)
# 去除字符串两边的空格(包含换行符)。
print(s.str.split(" "))
Our Serie is:
0 C
1 Python
2 java
3 go
4 1125
5 javascript
dtype: str
0 [C, ]
1 [, Python]
2 [java]
3 [go]
4 [1125, ]
5 [javascript]
dtype: object
# 用给定的分隔符连接字符串元素。
import pandas as pd
import numpy as np
s = pd.Series(['C', 'Python', 'java', 'go', np.nan, '1125','javascript'])
print ("Our Serie is:")
print(s)
#会自动忽略NaN
print(s.str.cat(sep="_"))
Our Serie is:
0 C
1 Python
2 java
3 go
4 NaN
5 1125
6 javascript
dtype: str
C_Python_java_go_1125_javascript
# 返回一个带有独热编码值的 DataFrame 结构。
import pandas as pd
import numpy as np
s = pd.Series(['C', 'Python', 'java', 'go', np.nan, '1125','javascript'])
print ("Our Serie is:")
print(s)
print(s.str.get_dummies())
Our Serie is:
0 C
1 Python
2 java
3 go
4 NaN
5 1125
6 javascript
dtype: str
1125 C Python go java javascript
0 0 1 0 0 0 0
1 0 0 1 0 0 0
2 0 0 0 0 1 0
3 0 0 0 1 0 0
4 0 0 0 0 0 0
5 1 0 0 0 0 0
6 0 0 0 0 0 1
import pandas as pd
import numpy as np
s = pd.Series(['C ',' Python','java','go','1125 ','javascript'])
print ("Our Serie is:")
print(s)
print(s.str.contains(" "))
Our Serie is:
0 C
1 Python
2 java
3 go
4 1125
5 javascript
dtype: str
0 True
1 True
2 False
3 False
4 True
5 False
dtype: bool
import pandas as pd
import numpy as np
s = pd.Series(['C ',' Python','java','go','1125 ','javascript'])
print ("Our Serie is:")
print(s)
print(s.str.repeat(3))
Our Serie is:
0 C
1 Python
2 java
3 go
4 1125
5 javascript
dtype: str
0 C C C
1 Python Python Python
2 javajavajava
3 gogogo
4 1125 1125 1125
5 javascriptjavascriptjavascript
dtype: str
import pandas as pd
import numpy as np
s = pd.Series(['C ',' Python','java','go','1125 ','javascript'])
print ("Our Serie is:")
print(s)
#若以指定的"j"开头则返回True
print(s.str.startswith("j"))
Our Serie is:
0 C
1 Python
2 java
3 go
4 1125
5 javascript
dtype: str
0 False
1 False
2 True
3 False
4 False
5 True
dtype: bool
import pandas as pd
import numpy as np
s = pd.Series(['C ',' Python','java','go','1125 ','javascript'])
print(s.str.find("j"))
0 -1
1 -1
2 0
3 -1
4 -1
5 0
dtype: int64
import pandas as pd
import numpy as np
s = pd.Series(['C ',' Python','java','go','1125 ','javascript'])
print(s.str.findall("j"))
0 []
1 []
2 [j]
3 []
4 []
5 [j]
dtype: object
import pandas as pd
import numpy as np
s = pd.Series(['C ',' Python','java','go','1125 ','javascript'])
# 交换大小写
print(s.str.swapcase())
0 c
1 pYTHON
2 JAVA
3 GO
4 1125
5 JAVASCRIPT
dtype: str
import pandas as pd
import numpy as np
# 返回布尔值,检查 Series 中组成每个字符串的所有字符是否都为数字。
s = pd.Series(['C ',' Python','java','go','1125','javascript'])
print(s.str.isnumeric())
0 False
1 False
2 False
3 False
4 True
5 False
dtype: bool

浙公网安备 33010602011771号