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
类型。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串