微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

熊猫中的一种变换

如何解决熊猫中的一种变换

给定一个像这样一行的示例数据框:

brand_1 gen_1   both_1  brand_2 gen_2   both_2  brand_3 gen_3   both_3
6133    5636    5446    0       3239    0           6032    5870    5484

Transpose 或 Transform 在我尝试生成此数据的以下重塑时不起作用:

Type    Brand    Gen    Both
1       6133     5636   5446
2       0        3239   0
3       6032     5870   5484

我已经在大块的列上尝试了 Transform 和 Transpose 以及 iloc,但没有结果以这种方式重塑数据。因此,我的问题在这里

解决方法

让我们试试import cv2 import numpy as np from sklearn.cluster import KMeans from matplotlib import pyplot as plt image = cv2.imread(r"C:\Users\KaChun\Desktop\rotapple.jpg") reshaped = image.reshape(image.shape[0] * image.shape[1],image.shape[2]) wcss = [] for i in range(1,11): kmeans = KMeans(n_clusters=i,init ='k-means++',max_iter=300,n_init=10,random_state=0 ) kmeans.fit(reshaped) wcss.append(kmeans.inertia_) plt.plot(range(1,11),wcss) plt.title('The Elbow Method Graph') plt.xlabel('Number of clusters') plt.ylabel('WCSS') plt.show()

wide_to_long
,

您也可以使用 pivot_longer 中的 pyjanitor 函数;目前您必须从 github:

安装最新的开发版本
 # install latest dev version
 # pip install git+https://github.com/ericmjl/pyjanitor.git

 import janitor

df.pivot_longer(index = None,names_to = ('.value','Type'),names_sep = '_')

  Type  brand   gen  both
0    1   6133  5636  5446
1    2      0  3239     0
2    3   6032  5870  5484

.value 作为指标;列名中与 .value(brand,gen,both) 对齐的部分保留为列名,而与 Type 对齐的部分成为新列 Type 中的值。>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。