gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
pandas新增列时指定数据类型
- User
pandas新增列时指定数据类型
- Assistant
在pandas中,可以使用
astype函数来指定新增列的数据类型。下面是一个示例:
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3]}) # 新增列B,数据类型为字符串 df['B'] = df['A'].astype(str) print(df)输出结果为:
A B 0 1 1 1 2 2 2 3 3在上述示例中,我们使用
astype函数将列A的数据类型转换为字符串,并将其赋值给新增的列B。通过将astype(str)作为参数传递给astype函数,我们可以指定新增列的数据类型为字符串。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.