gzj137070928 发表于 2020-12-11 16:22:23

pandas的DataFrame的categeries

pandas的DataFrame的categeries
import pandas as pd
import time

idx =
name = ["apple","pearl","orange", "apple","orange","orange","apple","pearl","orange"]
price =
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)
页: [1]
查看完整版本: pandas的DataFrame的categeries