Pandas 将列值转换为字符串

Suraj Joshi 2023年1月30日 2021年1月22日
  1. 使用 apply() 方法将 DataFrame 的列值的数据类型转换为字符串
  2. 使用 applymap() 方法将所有 DataFrame 列的数据类型转换为 string
  3. 使用 astype() 方法将 DataFrame 列值的数据类型转换为 string
Pandas 将列值转换为字符串

本教程介绍了如何将 DataFrame 的列值的数据类型转换为字符串。

import pandas as pd

employees_df = pd.DataFrame({
    'Name': ["Ayush","Bikram","Ceela","Kusal","Shanty"],
    'Score': [31, 38, 33, 39,35],
    'Age':  [33,34,38,45,37],

})

print(employees_df)

输出:

     Name  Score  Age
0   Ayush     31   33
1  Bikram     38   34
2   Ceela     33   38
3   Kusal     39   45
4  Shanty     35   37

我们将使用上面例子中显示的 DataFrame 来解释如何将 DataFrame 的列值的数据类型转换为字符串。

使用 apply() 方法将 DataFrame 的列值的数据类型转换为字符串

import pandas as pd

employees_df = pd.DataFrame({
    'Name': ["Ayush","Bikram","Ceela","Kusal","Shanty"],
    'Score': [31, 38, 33, 39,35],
    'Age':  [33,34,38,45,37],

})
print("DataFrame before Conversion:")
print(employees_df,"\n")
print("Datatype of columns before conversion:")
print(employees_df.dtypes,"\n")

employees_df["Age"]=employees_df["Age"].apply(str)

print("DataFrame after conversion:")
print(employees_df,"\n")
print("Datatype of columns after conversion:")
print(employees_df.dtypes)

输出:

DataFrame before Conversion:
     Name  Score  Age
0   Ayush     31   33
1  Bikram     38   34
2   Ceela     33   38
3   Kusal     39   45
4  Shanty     35   37

Datatype of columns before conversion:
Name     object
Score     int64
Age       int64
dtype: object

DataFrame after conversion:
     Name  Score Age
0   Ayush     31  33
1  Bikram     38  34
2   Ceela     33  38
3   Kusal     39  45
4  Shanty     35  37

Datatype of columns after conversion:
Name     object
Score     int64
Age      object
dtype: object

它将 Age 列的数据类型从 int64 改为代表字符串的 object 类型。

使用 applymap() 方法将所有 DataFrame 列的数据类型转换为 string

如果我们想将 DataFrame 中所有列值的数据类型改为 string 类型,我们可以使用 applymap() 方法。

import pandas as pd

employees_df = pd.DataFrame({
    'Name': ["Ayush","Bikram","Ceela","Kusal","Shanty"],
    'Score': [31, 38, 33, 39,35],
    'Age':  [33,34,38,45,37],

})
print("DataFrame before Conversion:")
print(employees_df,"\n")
print("Datatype of columns before conversion:")
print(employees_df.dtypes,"\n")

employees_df=employees_df.applymap(str)

print("DataFrame after conversion:")
print(employees_df,"\n")
print("Datatype of columns after conversion:")
print(employees_df.dtypes)

输出:

DataFrame before Conversion:
     Name  Score  Age
0   Ayush     31   33
1  Bikram     38   34
2   Ceela     33   38
3   Kusal     39   45
4  Shanty     35   37
zeppy@zeppy-G7-7588:~/test/Week-01/taddaa$ python3 1.py
DataFrame before Conversion:
     Name  Score  Age
0   Ayush     31   33
1  Bikram     38   34
2   Ceela     33   38
3   Kusal     39   45
4  Shanty     35   37

Datatype of columns before conversion:
Name     object
Score     int64
Age       int64
dtype: object

DataFrame after conversion:
     Name Score Age
0   Ayush    31  33
1  Bikram    38  34
2   Ceela    33  38
3   Kusal    39  45
4  Shanty    35  37

Datatype of columns after conversion:
Name     object
Score    object
Age      object
dtype: object

它将所有 DataFrame 列的数据类型转换为 string 类型,在输出中用 object 表示。

使用 astype() 方法将 DataFrame 列值的数据类型转换为 string

import pandas as pd

employees_df = pd.DataFrame({
    'Name': ["Ayush","Bikram","Ceela","Kusal","Shanty"],
    'Score': [31, 38, 33, 39,35],
    'Age':  [33,34,38,45,37],

})
print("DataFrame before Conversion:")
print(employees_df,"\n")
print("Datatype of columns before conversion:")
print(employees_df.dtypes,"\n")

employees_df["Score"]=employees_df["Score"].astype(str)

print("DataFrame after conversion:")
print(employees_df,"\n")
print("Datatype of columns after conversion:")
print(employees_df.dtypes)

输出:

DataFrame before Conversion:
     Name  Score  Age
0   Ayush     31   33
1  Bikram     38   34
2   Ceela     33   38
3   Kusal     39   45
4  Shanty     35   37

Datatype of columns before conversion:
Name     object
Score     int64
Age       int64
dtype: object

DataFrame after conversion:
     Name Score  Age
0   Ayush    31   33
1  Bikram    38   34
2   Ceela    33   38
3   Kusal    39   45
4  Shanty    35   37

Datatype of columns after conversion:
Name     object
Score    object
Age       int64
dtype: object

它将 employees_df Dataframe 中 Score 列的数据类型转换为 string 类型。

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相关文章 - Pandas DataFrame Column