鱼C论坛

 找回密码
 立即注册
查看: 2499|回复: 0

[技术交流] pandas的DataFrame的categeries

[复制链接]
发表于 2020-12-11 16:22:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
pandas的DataFrame的categeries
import pandas as pd
import time

idx = [1,2,3,5,6,7,9,4,8]
name = ["apple","pearl","orange", "apple","orange","orange","apple","pearl","orange"]
price = [5.20,3.50,7.30,5.00,7.50,7.30,5.20,3.70,7.30]
N = 10  # 数据越大,内存差距越大
df = pd.DataFrame({ "fruit": name*N , "price" : price*N}, index = idx*N)
print (df,"\n")
print ('memory_usage',df.memory_usage(),"\n")
print (df.dtypes)
print ("*" * 20)
df['fruit'] = df['fruit'].astype('category')
# 将fruit列由Series改为了category类型,通过codes和categories组合出fruit的values
print (df)
print ('memory_usage',df.memory_usage(),"\n")
print (df.dtypes)
print('fruit.values:',df.fruit.values)
print('fruit.values.codes:',df.fruit.values.codes)
print('fruit.values.categories:',df.fruit.values.categories)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-17 02:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表