📜  Python|熊猫 Series.as_blocks()

📅  最后修改于: 2022-05-13 01:54:22.229000             🧑  作者: Mango

Python|熊猫 Series.as_blocks()

Pandas 系列是带有轴标签的一维 ndarray。标签不必是唯一的,但必须是可散列的类型。该对象支持基于整数和基于标签的索引,并提供了许多用于执行涉及索引的操作的方法。

Pandas Series.as_blocks()函数用于将帧转换为 dtype -> Constructor Types 的字典,每个都具有同质的 dtype。

示例 #1:使用Series.as_blocks()函数将给定的系列对象作为字典返回。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio'])
  
# Create the Index
index_ = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5'] 
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

输出 :

City 1    New York
City 2     Chicago
City 3     Toronto
City 4      Lisbon
City 5         Rio
dtype: object

现在我们将使用Series.as_blocks()函数将给定的系列对象作为字典返回。

# return a dictionary
result = sr.as_blocks()
  
# Print the result
print(result)

输出 :

{'object': City 1    New York
City 2     Chicago
City 3     Toronto
City 4      Lisbon
City 5         Rio
dtype: object}

正如我们在输出中看到的, Series.as_blocks()函数已成功将给定的系列对象作为字典返回。示例 #2:使用Series.as_blocks()函数将给定的系列对象作为字典返回。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([11, 21, 8, 18, 65, 18, 32, 10, 5, 32, None])
  
# Create the Index
# apply yearly frequency
index_ = pd.date_range('2010-10-09 08:45', periods = 11, freq ='Y')
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

输出 :

2010-12-31 08:45:00    11.0
2011-12-31 08:45:00    21.0
2012-12-31 08:45:00     8.0
2013-12-31 08:45:00    18.0
2014-12-31 08:45:00    65.0
2015-12-31 08:45:00    18.0
2016-12-31 08:45:00    32.0
2017-12-31 08:45:00    10.0
2018-12-31 08:45:00     5.0
2019-12-31 08:45:00    32.0
2020-12-31 08:45:00     NaN
Freq: A-DEC, dtype: float64

现在我们将使用Series.as_blocks()函数将给定的系列对象作为字典返回。

# return a dictionary
result = sr.as_blocks()
  
# Print the result
print(result)

输出 :

{'float64': 2010-12-31 08:45:00    11.0
2011-12-31 08:45:00    21.0
2012-12-31 08:45:00     8.0
2013-12-31 08:45:00    18.0
2014-12-31 08:45:00    65.0
2015-12-31 08:45:00    18.0
2016-12-31 08:45:00    32.0
2017-12-31 08:45:00    10.0
2018-12-31 08:45:00     5.0
2019-12-31 08:45:00    32.0
2020-12-31 08:45:00     NaN
Freq: A-DEC, dtype: float64}

正如我们在输出中看到的, Series.as_blocks()函数已成功将给定的系列对象作为字典返回。