python .center用法_Python Pandas Series.str.center()用法及代码示例

Series.str可用于以字符串形式访问系列的值并对其应用几种方法。 Pandas Series.str.center()函数用于在系列/索引中的字符串的左侧和右侧填充其他字符。该功能等效于Python的str.center()。

用法: Series.str.center(width, fillchar=’ ‘)

参数:

width:产生的字符串的最小宽度;其他字符将用fillchar填充

fillchar:填充字符,默认为空白

返回:填充

范例1:采用Series.str.center()函数用“ *”符号填充给定系列对象的基础数据中字符串的左侧和右侧。

# importing pandas as pd

import pandas as pd

# Creating the Series

sr = pd.Series(['New_York', 'Lisbon', 'Tokyo', 'Paris', 'Munich'])

# Creating the index

idx = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']

# set the index

sr.index = idx

# Print the series

print(sr)

输出:

0428202531c43dbacc9932adc9fd1c8f.png

现在我们将使用Series.str.center()函数在字符串的左侧和右侧填充“ *”符号。

# fill '*' in the left and right side of string

result = sr.str.center(width = 13, fillchar = '*')

# print the result

print(result)

输出:

086eae04e599ba2e9a6c9d961d008d27.png

正如我们在输出中看到的,Series.str.center()函数已成功在给定系列对象的基础数据中的字符串的左侧和右侧填充了“ *”符号。

范例2:采用Series.str.center()函数用“ *”符号填充给定系列对象的基础数据中字符串的左侧和右侧。

# importing pandas as pd

import pandas as pd

# Creating the Series

sr = pd.Series(['Mike', 'Alessa', 'Nick', 'Kim', 'Britney'])

# Creating the index

idx = ['Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5']

# set the index

sr.index = idx

# Print the series

print(sr)

输出:

995956efe96c1b37333cf3b2bda63d83.png

现在我们将使用Series.str.center()函数在字符串的左侧和右侧填充“ *”符号。

# fill '*' in the left and right side of string

# width after filling should be 5

result = sr.str.center(width = 5, fillchar = '*')

# print the result

print(result)

输出:

49d46e4eb8edcc769c67cd3fae698a25.png

正如我们在输出中看到的,Series.str.center()函数已成功在给定系列对象的基础数据中的字符串的左侧和右侧填充了“ *”符号。

注意:如果width的值小于实际字符串的长度,则整个字符串将被打印而不会被截断。

import numpy as np import pandas as pd from sklearn.cluster import KMeans from sklearn.preprocessing import StandardScaler from scipy.spatial.distance import cdist import matplotlib.pyplot as plt from pandas import DataFrame from sklearn.decomposition import PCA plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 pd.set_option('display.max_rows', None)#显示全部行 pd.set_option('display.max_columns', None)#显示全部列 np.set_printoptions(threshold=np.inf) pd.set_option('display.max_columns', 9000) pd.set_option('display.width', 9000) pd.set_option('display.max_colwidth', 9000) df = pd.read_csv(r'附件1.csv',encoding='gbk') X = np.array(df.iloc[:, 1:]) X=X[0:,1:] k=93 kmeans_model = KMeans(n_clusters=k, random_state=123) fit_kmeans = kmeans_model.fit(X) # 模型训练 #查看聚类结果 kmeans_cc = kmeans_model.cluster_centers_ # 聚类中心 print('各类聚类中心为:\n', kmeans_cc) kmeans_labels = kmeans_model.labels_ # 样本的类别标签 print('各样本的类别标签为:\n', kmeans_labels) r1 = pd.Series(kmeans_model.labels_).value_counts() # 统计不同类别样本的数目 print('最终每个类别的数目为:\n', r1) # 输出聚类分群的结果 # cluster_center = pd.DataFrame(kmeans_model.cluster_centers_, # columns=[ str(x) for x in range(1,94)]) # 将聚类中心放在数据框中 # cluster_center.index = pd.DataFrame(kmeans_model.labels_). \ # drop_duplicates().iloc[:, 0] # 将样本类别作为数据框索引 # print(cluster_center)代码解释
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值