熊猫:将列强制转换为字符串不起作用

熊猫:将列强制转换为字符串不起作用,第1张

熊猫:将列强制转换为字符串不起作用

dtype
string
dict
list
始终是
object
,测试
type
需要选择列例如,由一些价值
iat

type(resultstatsDF['file'].iat[0])

样品

resultstatsDF = pd.Dataframe({'file':['a','d','f']})print (resultstatsDF)  file0    a1    d2    fprint (type(resultstatsDF['file'].iloc[0]))<class 'str'>print (resultstatsDF['file'].apply(type))0    <class 'str'>1    <class 'str'>2    <class 'str'>Name: file, dtype: object

样品:

df = pd.Dataframe({'strings':['a','d','f'],        'dicts':[{'a':4}, {'c':8}, {'e':9}],        'lists':[[4,8],[7,8],[3]],        'tuples':[(4,8),(7,8),(3,)],        'sets':[set([1,8]), set([7,3]), set([0,1])] })print (df)      dicts   lists    sets strings  tuples0  {'a': 4}  [4, 8]  {8, 1}       a  (4, 8)1  {'c': 8}  [7, 8]  {3, 7}       d  (7, 8)2  {'e': 9}     [3]  {0, 1}       f    (3,)

所有值都相同

dtypes

print (df.dtypes)dicts      objectlists      objectsets       objectstrings    objecttuples     objectdtype: object

但是

type
不同,如果需要循环检查:

for col in df:    print (df[col].apply(type))0    <class 'dict'>1    <class 'dict'>2    <class 'dict'>Name: dicts, dtype: object0    <class 'list'>1    <class 'list'>2    <class 'list'>Name: lists, dtype: object0    <class 'set'>1    <class 'set'>2    <class 'set'>Name: sets, dtype: object0    <class 'str'>1    <class 'str'>2    <class 'str'>Name: strings, dtype: object0    <class 'tuple'>1    <class 'tuple'>2    <class 'tuple'>Name: tuples, dtype: object

或列的第一个值:

print (type(df['strings'].iat[0]))<class 'str'>print (type(df['dicts'].iat[0]))<class 'dict'>print (type(df['lists'].iat[0]))<class 'list'>print (type(df['tuples'].iat[0]))<class 'tuple'>print (type(df['sets'].iat[0]))<class 'set'>

boolean indexing
如果可能混合柱(然后一些熊猫功能可以分成)是可能的过滤器通过
type

df = pd.Dataframe({'mixed':['3', 5, 9,'2']})print (df)  mixed0     31     52     93     2print (df.dtypes)mixed    objectdtype: object

for col in df:    print (df[col].apply(type))0    <class 'str'>1    <class 'int'>2    <class 'int'>3    <class 'str'>Name: mixed, dtype: object#python 3 - string#python 2 - basestringmask = df['mixed'].apply(lambda x: isinstance(x,str))print (mask)0     True1    False2    False3     TrueName: mixed, dtype: booldf = df[mask]print (df)  mixed0     33     2


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5629826.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存