pandas读存列表、字典以及其它数据类型

博客介绍了pandas读存数据类型的情况,pandas每列除能存入float、str、int等常见类型,还可存入list、dict及其他类型元素,实际上任何类型元素都能正常写入pandas。

pandas读存列表字典即其它数据类型

pandas的每列除了存入常见的float,str,int外,还可以存入list,dict以及其他类型的元素.
事实上,无论什么类型元素都可以正常写入pandas当中。

import pandas as pd

class CGAI(object):

    def __init__(self,num):
        super(CGAI, self).__init__()



a={'A':[1,2,3],'B':[4,5,6],'C':['q q','w w','e e'],'E':[{'a':1},{'b':2},{'c':3}],'F':[CGAI(1),CGAI(2),CGAI(3)]}

df=pd.DataFrame(a)
print(df)
'''
   A  B    C         E                                             F
0  1  4  q q  {'a': 1}  <__main__.CGAI object at 0x000002A4F6C55550>
1  2  5  w w  {'b': 2}  <__main__.CGAI object at 0x000002A4F6C555C0>
2  3  6  e e  {'c': 3}  <__main__.CGAI object at 0x000002A4F6C555F8>
'''


def clip(text):
    return text.split(' ')

#写入列表
df['D']= df['C'].apply(clip)  #在列基础上计算后再将值添加到新列上
print(df)
'''
   A  B    C         E                                             F       D
0  1  4  q q  {'a': 1}  <__main__.CGAI object at 0x000002A4F6C55550>  [q, q]
1  2  5  w w  {'b': 2}  <__main__.CGAI object at 0x000002A4F6C555C0>  [w, w]
2  3  6  e e  {'c': 3}  <__main__.CGAI object at 0x000002A4F6C555F8>  [e, e]
'''



v=df['D'].values  #读取列表
print(v[0])
print(type(v[0]))
'''
['q', 'q']
<class 'list'>
'''


d=df['E'].values  #读取字典
print(d[0])
print(type(d[0]))
'''
{'a': 1}
<class 'dict'>
'''

f=df['F'].values  #读取其他类型元素
print(f[0])
print(type(f[0]))
'''
<__main__.CGAI object at 0x000002A4F6C55550>
<class '__main__.CGAI'>
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值