list,dict,array,series,dataframe相互转换

1.dict to series/dataframe

#dict
data = { 'words': ['animal', 'ambition', 'balance', 'city', 'decade'],
         'year': [2000, 2001, 2002, 2001, 2002],
         'number': [1.5, 1.7, 3.6, 2.4, 2.9]} 
#print(type(data))

#dict to series
#Series是一个一维的类似的数组对象,包含一个数组的数据(任何NumPy的数据类型)和一个与数组关联的数据标签,被叫做 索引 。
#若不指定 index,data 的 key 充当 Series 的 index
ser=Series(data)
print(ser.index)
#输出为:Index(['words', 'year', 'number'], dtype='object')
print(ser)
#输出为:
#words     [animal, ambition, balance, city, decade]
#year                 [2000, 2001, 2002, 2001, 2002]
#number                    [1.5, 1.7, 3.6, 2.4, 2.9]
#dtype: object

#dict to dataframe
df = DataFrame(data)
print(df)
#输出为:
#      words  year  number
#0    animal  2000     1.5
#1  ambition  2001     1.7
#2   balance  2002     3.6
#3      city  2001     2.4
#4    decade  2002     2.9

2.list to series/dataframe/array

import numpy as np
import pandas as pd
from pandas import DataFrame

data=[["animal",2000,1.5],["ambition",2001,1.7],["balance",2002,3.6],["city",2001,2.4],["decade",2002,2.9]]
#list to series
ser=Series(data)
print(ser)
#输出为:
#0      [animal, 2000, 1.5]
#1    [ambition, 2001, 1.7]
#2     [balance, 2002, 3.6]
#3        [city, 2001, 2.4]
#4      [decade, 2002, 2.9]
#dtype: object
#或者指定series的index
ser=Series(data,index=["A","B","C","D","E"])
print(ser)
#输出为:
#A      [animal, 2000, 1.5]
#B    [ambition, 2001, 1.7]
#C     [balance, 2002, 3.6]
#D        [city, 2001, 2.4]
#E      [decade, 2002, 2.9]
#dtype: object
#series to list
a_list=ser.tolist()
print(a_list)
#输出为:[['animal', 2000, 1.5], ['ambition', 2001, 1.7], ['balance', 2002, 3.6], ['city', 2001, 2.4], ['decade', 2002, 2.9]]

#list to dataframe
df=DataFrame(data,index=["A","B","C","D","E"],columns=['words', 'year', 'number'])
print(df)
#输出为:
#      words  year  number
#A    animal  2000     1.5
#B  ambition  2001     1.7
#C   balance  2002     3.6
#D      city  2001     2.4
#E    decade  2002     2.9

#list to array
ndarray=np.array(data)
print(ndarray)
#输出为:
#[['animal' '2000' '1.5']
# ['ambition' '2001' '1.7']
# ['balance' '2002' '3.6']
# ['city' '2001' '2.4']
# ['decade' '2002' '2.9']]
print(ndarray.shape)
#输出为:(5, 3)

3.array to dataframe

import numpy as np
import pandas as pd
from pandas import DataFrame

data=[["animal",2000,1.5],["ambition",2001,1.7],["balance",2002,3.6],["city",2001,2.4],["decade",2002,2.9]]
#list to array
ndarray=np.array(data)
"""
输出为:
[['animal' '2000' '1.5']
 ['ambition' '2001' '1.7']
 ['balance' '2002' '3.6']
 ['city' '2001' '2.4']
 ['decade' '2002' '2.9']]
"""
#arrary to dataframe
pd=DataFrame(ndarray,index=["A","B","C","D","E"],columns=['words', 'year', 'number'])
print(pd)
#输出为:
#      words  year number
#A    animal  2000    1.5
#B  ambition  2001    1.7
#C   balance  2002    3.6
#D      city  2001    2.4
#E    decade  2002    2.9

4.dataframe to array/dict

import numpy as np
import pandas as pd
from pandas import DataFrame

data=[["animal",2000,1.5],["ambition",2001,1.7],["balance",2002,3.6],["city",2001,2.4],["decade",2002,2.9]]
pd=DataFrame(data,index=["A","B","C","D","E"],columns=['words', 'year', 'number'])
#dataframe to array
ndarray=np.array(pd)
print(ndarray)
print(ndarray.shape)
#输出为:
#[['animal' 2000 1.5]
# ['ambition' 2001 1.7]
# ['balance' 2002 3.6]
# ['city' 2001 2.4]
# ['decade' 2002 2.9]]
#(5, 3)


#dataframe to dict
#dict返回的是dict of dict;list返回的是列表的字典;series返回的是序列的字典;records返回的是字典的列表
dict_data1=pd.to_dict(orient="dict")
print(dict_data1)
#输出为:{'words': {'A': 'animal', 'B': 'ambition', 'C': 'balance', 'D': 'city', 'E': 'decade'}, 'year': {'A': 2000, 'B': 2001, 'C': 2002, 'D': 2001, 'E': 2002}, 'number': {'A': 1.5, 'B': 1.7, 'C': 3.6, 'D': 2.4, 'E': 2.9}}
dict_data2=pd.to_dict(orient="list")
print(dict_data2)
#输出为:{'words': ['animal', 'ambition', 'balance', 'city', 'decade'], 'year': [2000, 2001, 2002, 2001, 2002], 'number': [1.5, 1.7, 3.6, 2.4, 2.9]}
dict_data3=pd.to_dict(orient="series")
print(dict_data3)
#输出为:
#{'words': A      animal
#B    ambition
#C     balance
#D        city
#E      decade
#Name: words, dtype: object, 'year': A    2000
#B    2001
#C    2002
#D    2001
#E    2002
#Name: year, dtype: int64, 'number': A    1.5
#B    1.7
#C    3.6
#D    2.4
#E    2.9
#Name: number, dtype: float64}
dict_data4= pd.to_dict(orient='records')
print(dict_data4)
#输出为:[{'words': 'animal', 'year': 2000, 'number': 1.5}, {'words': 'ambition', 'year': 2001, 'number': 1.7}, {'words': 'balance', 'year': 2002, 'number': 3.6}, {'words': 'city', 'year': 2001, 'number': 2.4}, {'words': 'decade', 'year': 2002, 'number': 2.9}]

对于多列的dataframe,把第一列当作key,其他列当作value

import numpy as np
import pandas as pd
from pandas import DataFrame
data=[["animal",2000,1.5],["ambition",2001,1.7],["balance",2002,3.6],["city",2001,2.4],["decade",2002,2.9]]
pd=DataFrame(data,index=["A","B","C","D","E"],columns=['words', 'year', 'number'])
dict_data1=pd.set_index("words").T.to_dict("list")
print(dict_data1)
#输出为:{'animal': [2000.0, 1.5], 'ambition': [2001.0, 1.7], 'balance': [2002.0, 3.6], 'city': [2001.0, 2.4], 'decade': [2002.0, 2.9]}

参考:https://yam.gift/2017/02/15/2017-02-15-list-dict-series-dataframe-ndarray-transform/

以上,记录本人学习过程。

  • 6
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值