如何将 pandas 数据框的单列转换为字符串类型?在下面的住房数据 df 中,我需要将邮政编码转换为字符串,以便当我运行线性回归时,邮政编码被视为分类而不是数字。谢谢!
df = pd.DataFrame({'zipcode': {17384: 98125, 2680: 98107, 722: 98005, 18754: 98109, 14554: 98155}, 'bathrooms': {17384: 1.5, 2680: 0.75, 722: 3.25, 18754: 1.0, 14554: 2.5}, 'sqft_lot': {17384: 1650, 2680: 3700, 722: 51836, 18754: 2640, 14554: 9603}, 'bedrooms': {17384: 2, 2680: 2, 722: 4, 18754: 2, 14554: 4}, 'sqft_living': {17384: 1430, 2680: 1440, 722: 4670, 18754: 1130, 14554: 3180}, 'floors': {17384: 3.0, 2680: 1.0, 722: 2.0, 18754: 1.0, 14554: 2.0}})
print (df)
bathrooms bedrooms floors sqft_living sqft_lot zipcode
722 3.25 4 2.0 4670 51836 98005
2680 0.75 2 1.0 1440 3700 98107
14554 2.50 4 2.0 3180 9603 98155
17384 1.50 2 3.0 1430 1650 98125
18754 1.00 2 1.0 1130 2640 98109
astype
:
df['zipcode'] = df.zipcode.astype(str)
#df.zipcode = df.zipcode.astype(str)
用于转换为
categorical
:
df['zipcode'] = df.zipcode.astype('category')
#df.zipcode = df.zipcode.astype('category')
Categorical
:
df['zipcode'] = pd.Categorical(df.zipcode)
数据样本:
import pandas as pd
df = pd.DataFrame({'zipcode': {17384: 98125, 2680: 98107, 722: 98005, 18754: 98109, 14554: 98155}, 'bathrooms': {17384: 1.5, 2680: 0.75, 722: 3.25, 18754: 1.0, 14554: 2.5}, 'sqft_lot': {17384: 1650, 2680: 3700, 722: 51836, 18754: 2640, 14554: 9603}, 'bedrooms': {17384: 2, 2680: 2, 722: 4, 18754: 2, 14554: 4}, 'sqft_living': {17384: 1430, 2680: 1440, 722: 4670, 18754: 1130, 14554: 3180}, 'floors': {17384: 3.0, 2680: 1.0, 722: 2.0, 18754: 1.0, 14554: 2.0}})
print (df)
bathrooms bedrooms floors sqft_living sqft_lot zipcode
722 3.25 4 2.0 4670 51836 98005
2680 0.75 2 1.0 1440 3700 98107
14554 2.50 4 2.0 3180 9603 98155
17384 1.50 2 3.0 1430 1650 98125
18754 1.00 2 1.0 1130 2640 98109
print (df.dtypes)
bathrooms float64
bedrooms int64
floors float64
sqft_living int64
sqft_lot int64
zipcode int64
dtype: object
df['zipcode'] = df.zipcode.astype('category')
print (df)
bathrooms bedrooms floors sqft_living sqft_lot zipcode
722 3.25 4 2.0 4670 51836 98005
2680 0.75 2 1.0 1440 3700 98107
14554 2.50 4 2.0 3180 9603 98155
17384 1.50 2 3.0 1430 1650 98125
18754 1.00 2 1.0 1130 2640 98109
print (df.dtypes)
bathrooms float64
bedrooms int64
floors float64
sqft_living int64
sqft_lot int64
zipcode category
dtype: object
随着 pandas >= 1.0,现在有一个专用的字符串数据类型:
1) 您可以使用 .astype('string'): 将列转换为 pandas string 数据类型
df['zipcode'] = df['zipcode'].astype('string')
2) 这与使用
str
不同,后者设置 pandas 对象数据类型:
df['zipcode'] = df['zipcode'].astype(str)
3) 要更改为分类数据类型,请使用:
df['zipcode'] = df['zipcode'].astype('category')
当您查看数据帧的信息时,您可以看到数据类型的差异:
df = pd.DataFrame({
'zipcode_str': [90210, 90211] ,
'zipcode_string': [90210, 90211],
'zipcode_category': [90210, 90211],
})
df['zipcode_str'] = df['zipcode_str'].astype(str)
df['zipcode_string'] = df['zipcode_str'].astype('string')
df['zipcode_category'] = df['zipcode_category'].astype('category')
df.info()
# you can see that the first column has dtype object
# while the second column has the new dtype string
# the third column has dtype category
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 zipcode_str 2 non-null object
1 zipcode_string 2 non-null string
2 zipcode_category 2 non-null category
dtypes: category(1), object(1), string(1)
“string”扩展类型解决了 object-dtype 的几个问题 NumPy 数组:
您可能会意外地将字符串和非字符串混合存储在 对象数据类型数组。 StringArray 只能存储字符串。
object dtype 会破坏特定于 dtype 的操作,例如 DataFrame.select_dtypes()。没有明确的方法来仅选择文本 虽然排除非文本,但仍然是对象数据类型列。
阅读代码时,对象数据类型数组的内容不太清晰 比字符串。
有关使用新字符串数据类型的更多信息可以在此处找到: https://pandas.pydata.org/pandas-docs/stable/user_guide/text.html
之前的答案集中在名义数据(例如无序)。如果有理由对序数变量强加顺序,那么可以使用:
# Transform to category
df['zipcode_category'] = df['zipcode_category'].astype('category')
# Add ordered category
df['zipcode_ordered'] = df['zipcode_category']
# Setup the ordering
df.zipcode_ordered.cat.set_categories(
new_categories = [90211, 90210], ordered = True, inplace = True
)
# Output IDs
df['zipcode_ordered_id'] = df.zipcode_ordered.cat.codes
print(df)
# zipcode_category zipcode_ordered zipcode_ordered_id
# 90210 90210 1
# 90211 90211 0
有关设置排序类别的更多详细信息,请访问 pandas 网站:
https://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html#sorting-and-order
要将列转换为字符串类型(这将是 pandas 中的 object 列本身),请使用
astype
:
df.zipcode = zipcode.astype(str)
如果想要获取
Categorical
列,可以将参数'category'
传递给函数:
df.zipcode = zipcode.astype('category')
将每个
object
列转换为 categorical
df = df.apply(lambda col: col.astype('category') if col.dtypes == 'object' else col)