向数据帧添加具有单个分类值的列

2024-10-03 13:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pandas.DataFrame{},想添加一个新列col,其中有一个值"hello"。我希望本专栏的数据类型为category,单一类别为"hello"。我可以做到以下几点

df["col"] = "hello"
df["col"] = df["col"].astype("catgegory")
  1. 我真的需要写df["col"]三次才能达到这个目的吗
  2. 在第一行之后,我担心中间数据帧df在新列转换为category之前可能会占用大量空间。(数据帧相当大,有数百万行,值"hello"实际上是一个更长的字符串。)

在避免上述问题的同时,是否还有其他直接的、简短的、快速的方法来实现这一点

另一种解决办法是

df["col"] = pd.Categorical(itertools.repeat("hello", len(df)))

但是它需要itertoolslen(df)的使用,我不确定内存使用情况如何


Tags: 数据目的hellodataframepandasdflencol
3条回答

这个解决方案肯定能解决第一点,但不一定能解决第二点:

df['col'] = pd.Categorical(('hello' for i in len(df)))

本质上

  • 我们首先创建一个长度等于df中记录数的“hello”生成器
  • 然后我们将其传递给pd.Categorical,使其成为一个分类列

一种简单的方法是使用df.assign创建新变量,然后使用df.astype以及特定列的数据类型字典将数据类型更改为category

df = df.assign(col="hello").astype({'col':'category'})

df.dtypes
A         int64
col    category
dtype: object

这样,您就不必创建一系列长度等于数据帧的数据。您可以直接广播输入字符串,这样会节省一些时间和内存


正如您所看到的,这种方法是非常可伸缩的。您可以根据需要分配多个变量,有些变量还基于复杂函数。然后根据需要为它们设置数据类型

df = pd.DataFrame({'A':[1,2,3,4]})

df = (df.assign(col1 = 'hello',                    #Define column based on series or broadcasting
                col2 = lambda x:x['A']**2,         #Define column based on existing columns
                col3 = lambda x:x['col2']/x['A'])  #Define column based on previously defined columns
        .astype({'col1':'category',
                 'col2':'float'}))

print(df)
print(df.dtypes)
   A   col1  col2  col3
0  1  hello   1.0   1.0
1  2  hello   4.0   2.0
2  3  hello   9.0   3.0
3  4  hello  16.0   4.0


A          int64
col1    category  #<-changed dtype
col2     float64  #<-changed dtype
col3     float64
dtype: object

我们可以显式构建正确大小和类型的序列,而不是通过__setitem__隐式构建,然后转换:

df['col'] = pd.Series('hello', index=df.index, dtype='category')

示例程序:

import pandas as pd

df = pd.DataFrame({'a': [1, 2, 3]})

df['col'] = pd.Series('hello', index=df.index, dtype='category')

print(df)
print(df.dtypes)
print(df['col'].cat.categories)
   a    col
0  1  hello
1  2  hello
2  3  hello

a         int64
col    category
dtype: object

Index(['hello'], dtype='object')

相关问题 更多 >