简体   繁体   English

用i替换已排序的Pandas数据框列中的每个唯一值

[英]Replace each unique value in a sorted Pandas dataframe column with i

I have a pandas dataframe with a list of user IDs that are about 40 characters long. 我有一个熊猫数据框,其中列出了大约40个字符长的用户ID。 I want to replace those user IDs with a number i starting from 0 for each id in order to save space. 我想将这些用户ID替换为每个ID从0开始的数字i,以节省空间。

What I have: 我有的:

userID      itemID
------------------
3a            r5
3a            r6
4b            r5
4c            r6

What I need: 我需要的:

 userID      itemID
 ------------------
 0            r5
 0            r6
 1            r5
 2            r6

use pd.factorize() : 使用pd.factorize()

In [145]: df
Out[145]:
  userID itemID
0     3a     r5
1     3a     r6
2     4b     r5
3     4c     r6

In [146]: df.userID = pd.factorize(df.userID)[0]

In [147]: df
Out[147]:
   userID itemID
0       0     r5
1       0     r6
2       1     r5
3       2     r6

if your main goal is to save memory - you can categorize your column: 如果您的主要目标是节省内存,则可以对列进行分类:

In [155]: df = pd.concat([df] * 5, ignore_index=True)

In [156]: df
Out[156]:
   userID itemID
0      3a     r5
1      3a     r6
2      4b     r5
3      4c     r6
4      3a     r5
5      3a     r6
6      4b     r5
7      4c     r6
8      3a     r5
9      3a     r6
10     4b     r5
11     4c     r6
12     3a     r5
13     3a     r6
14     4b     r5
15     4c     r6
16     3a     r5
17     3a     r6
18     4b     r5
19     4c     r6

In [157]: df.memory_usage()
Out[157]:
Index      80
userID    160
itemID    160
dtype: int64

categorizing userID : userID分类:

In [158]: df.userID = df.userID.astype('category')

In [159]: df.memory_usage()
Out[159]:
Index      80
userID     44    # <------------ NOTE:
itemID    160
dtype: int64

In [160]: df
Out[160]:
   userID itemID
0      3a     r5
1      3a     r6
2      4b     r5
3      4c     r6
4      3a     r5
5      3a     r6
6      4b     r5
7      4c     r6
8      3a     r5
9      3a     r6
10     4b     r5
11     4c     r6
12     3a     r5
13     3a     r6
14     4b     r5
15     4c     r6
16     3a     r5
17     3a     r6
18     4b     r5
19     4c     r6

In [161]: df.dtypes
Out[161]:
userID    category
itemID      object
dtype: object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用数组的每个值替换pandas数据框的每一列 - replace each column of pandas dataframe with each value of array 将 pandas DataFrame 中的一行替换为基于唯一列值的 dict 项 - Replace a row in a pandas DataFrame with a dict item based on a unique column value 按每个唯一列值的最近日期过滤 Pandas 数据框 - Filter Pandas dataframe by most recent date for each unique column value 对于 Pandas DataFrame 列中的每个唯一值,如何随机选择一定比例的行? - For each unique value in a pandas DataFrame column, how can I randomly select a proportion of rows? 熊猫数据框替换列中的唯一值 - Pandas dataframe replace unique values in a column 对唯一列值进行分组以获得 pandas dataframe 列中每个唯一值的平均值 - Grouping unique column values to get average of each unique value in pandas dataframe column 将唯一列值分组为 pandas dataframe 列中每个唯一值的总和 - Grouping unique column values to sum of each unique value in pandas dataframe column 有没有办法向pandas数据框添加新列,将新列的每个唯一值附加到数据帧的每个现有行? - Is there a way to add a new column to a pandas dataframe, appending each unique value of the new column to every existing row of the dataframe? Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column 将 pandas 数据框列中的每个值与第二个数据框列的所有值相乘并将每个第一个数据框值替换为结果数组 - Multiply each value in a pandas dataframe column with all values of 2nd dataframe column & replace each 1st dataframe value with resulting array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM