将 UUID 添加到 pandas DF
Add UUID's to pandas DF
假设我有一个像这样的 pandas DataFrame:
df = pd.DataFrame({'Name': ['John Doe', 'Jane Smith', 'John Doe', 'Jane Smith','Jack Dawson','John Doe']})
df:
Name
0 John Doe
1 Jane Smith
2 John Doe
3 Jane Smith
4 Jack Dawson
5 John Doe
而且我想添加一个列,如果名称相同,uuid 也相同。比如上面的DataFrame应该变成:
df:
Name UUID
0 John Doe 6d07cb5f-7faa-4893-9bad-d85d3c192f52
1 Jane Smith a709bd1a-5f98-4d29-81a8-09de6e675b56
2 John Doe 6d07cb5f-7faa-4893-9bad-d85d3c192f52
3 Jane Smith a709bd1a-5f98-4d29-81a8-09de6e675b56
4 Jack Dawson 6a495c95-dd68-4a7c-8109-43c2e32d5d42
5 John Doe 6d07cb5f-7faa-4893-9bad-d85d3c192f52
uuid 应该从 uuid.uuid4() 函数生成。
我目前的想法是使用 groupby("Name").cumcount() 来识别哪些行具有相同的名称,哪些行不同。然后我会创建一个字典,其中包含 cumcount 的键和 uuid 的值,并使用它来将 uuid 添加到 DF。
虽然这可行,但我想知道是否有更有效的方法来做到这一点?
这个怎么样
names = df['Name'].unique()
for name in names:
df.loc[df['Name'] == name, 'UUID'] = uuid.uuid4()
可以缩短为
for name in df['Name'].unique():
df.loc[df['Name'] == name, 'UUID'] = uuid.uuid4()
对数据框进行分组并应用 uuid.uuid4 将比遍历组更有效。由于您想保留数据框的原始形状,因此您应该使用 pandas
函数 transform
。
使用您的样本数据框,我们将添加一列以便有一个系列应用 transform
。由于 uuid.uuid4
不接受任何参数,因此列是什么并不重要。
df = pd.DataFrame({'Name': ['John Doe', 'Jane Smith', 'John Doe', 'Jane Smith','Jack Dawson','John Doe']})
df.loc[:, "UUID"] = 1
现在使用 transform
:
import uuid
df.loc[:, "UUID"] = df.groupby("Name").UUID.transform(lambda g: uuid.uuid4())
+----+--------------+--------------------------------------+
| | Name | UUID |
+----+--------------+--------------------------------------+
| 0 | John Doe | c032c629-b565-4903-be5c-81bf05804717 |
| 1 | Jane Smith | a5434e69-bd1c-4d29-8b14-3743c06e1941 |
| 2 | John Doe | c032c629-b565-4903-be5c-81bf05804717 |
| 3 | Jane Smith | a5434e69-bd1c-4d29-8b14-3743c06e1941 |
| 4 | Jack Dawson | 6b843d0f-ba3a-4880-8a84-d98c4af09cc3 |
| 5 | John Doe | c032c629-b565-4903-be5c-81bf05804717 |
+----+--------------+--------------------------------------+
uuid.uuid4
将被调用多次,因为有不同的组
假设我有一个像这样的 pandas DataFrame:
df = pd.DataFrame({'Name': ['John Doe', 'Jane Smith', 'John Doe', 'Jane Smith','Jack Dawson','John Doe']})
df:
Name
0 John Doe
1 Jane Smith
2 John Doe
3 Jane Smith
4 Jack Dawson
5 John Doe
而且我想添加一个列,如果名称相同,uuid 也相同。比如上面的DataFrame应该变成:
df:
Name UUID
0 John Doe 6d07cb5f-7faa-4893-9bad-d85d3c192f52
1 Jane Smith a709bd1a-5f98-4d29-81a8-09de6e675b56
2 John Doe 6d07cb5f-7faa-4893-9bad-d85d3c192f52
3 Jane Smith a709bd1a-5f98-4d29-81a8-09de6e675b56
4 Jack Dawson 6a495c95-dd68-4a7c-8109-43c2e32d5d42
5 John Doe 6d07cb5f-7faa-4893-9bad-d85d3c192f52
uuid 应该从 uuid.uuid4() 函数生成。
我目前的想法是使用 groupby("Name").cumcount() 来识别哪些行具有相同的名称,哪些行不同。然后我会创建一个字典,其中包含 cumcount 的键和 uuid 的值,并使用它来将 uuid 添加到 DF。
虽然这可行,但我想知道是否有更有效的方法来做到这一点?
这个怎么样
names = df['Name'].unique()
for name in names:
df.loc[df['Name'] == name, 'UUID'] = uuid.uuid4()
可以缩短为
for name in df['Name'].unique():
df.loc[df['Name'] == name, 'UUID'] = uuid.uuid4()
对数据框进行分组并应用 uuid.uuid4 将比遍历组更有效。由于您想保留数据框的原始形状,因此您应该使用 pandas
函数 transform
。
使用您的样本数据框,我们将添加一列以便有一个系列应用 transform
。由于 uuid.uuid4
不接受任何参数,因此列是什么并不重要。
df = pd.DataFrame({'Name': ['John Doe', 'Jane Smith', 'John Doe', 'Jane Smith','Jack Dawson','John Doe']})
df.loc[:, "UUID"] = 1
现在使用 transform
:
import uuid
df.loc[:, "UUID"] = df.groupby("Name").UUID.transform(lambda g: uuid.uuid4())
+----+--------------+--------------------------------------+
| | Name | UUID |
+----+--------------+--------------------------------------+
| 0 | John Doe | c032c629-b565-4903-be5c-81bf05804717 |
| 1 | Jane Smith | a5434e69-bd1c-4d29-8b14-3743c06e1941 |
| 2 | John Doe | c032c629-b565-4903-be5c-81bf05804717 |
| 3 | Jane Smith | a5434e69-bd1c-4d29-8b14-3743c06e1941 |
| 4 | Jack Dawson | 6b843d0f-ba3a-4880-8a84-d98c4af09cc3 |
| 5 | John Doe | c032c629-b565-4903-be5c-81bf05804717 |
+----+--------------+--------------------------------------+
uuid.uuid4
将被调用多次,因为有不同的组