gpt4 book ai didi

Python Pandas : how to obtain the datatypes of objects in a mixed-datatype column?

转载 作者:行者123 更新时间:2023-12-03 13:52:37 26 4
gpt4 key购买 nike

给定一个 pandas.DataFrame带有包含混合数据类型的列,例如

df = pd.DataFrame({'mixed': [pd.Timestamp('2020-10-04'), 999, 'a string']})
我想知道如何获取列(系列)中各个对象的数据类型?假设我想修改系列中某种类型的所有条目,比如将所有整数乘以某个因子。
我可以迭代地推导出一个掩码并在 loc 中使用它, 喜欢
m = np.array([isinstance(v, int) for v in df['mixed']])

df.loc[m, 'mixed'] *= 10

# df
# mixed
# 0 2020-10-04 00:00:00
# 1 9990
# 2 a string
这确实有用,但我想知道是否还有更多 pandas这样做的方式?

最佳答案

还需要电话type

m = df.mixed.map(lambda x : type(x).__name__)=='int'
df.loc[m, 'mixed']*=10
df
mixed
0 2020-10-04 00:00:00
1 9990
2 a string

关于Python Pandas : how to obtain the datatypes of objects in a mixed-datatype column?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64195782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com