人生最好的旅行,就是你在一个陌生的地方,发现一种久违的感动。你在看孤独的风景,你在逃避我的真心!
1、将一个字典输入:
该字典必须满足:value是一个list类型的元素,且每一个key对应的value长度都相同:
(以该字典的key为columns)
>>> import pandas as pd >>> a = [1,2,3,4,5] >>> b = ["a","b","c"] >>> c = 1 >>> df = pd.DataFrame({"A":a,"B":b,"C":c}) Traceback (most recent call last): ValueError: arrays must all be same length >>> df = pd.DataFrame([a,b]) # 作为list输入,list的元素必须也是list,加入c就错误 >>> df 0 1 2 3 4 0 1 2 3 4.0 5.0 1 a b c NaN NaN # 统一一下字典每个元素值的长度 >>> b = ["a","b","c","d","e"] >>> c = ("232","sdf","345","asd",1) >>> df = pd.DataFrame({"A":a,"B":b,"C":c}) >>> df A B C 0 1 a 232 1 2 b sdf 2 3 c 345 3 4 d asd 4 5 e 1
2、将多个key相同的字典列输入:
输入为一个list,该list各个元素为dict,且key可以不同(以含最多的key的字典的key为columns):
>>> d1 = {"A":1,"B":2,"C":3} >>> d2 = {"A":"a","B":"b",} >>> d3 = {"A":(1,2),"B":"ab","C":3} >>> li = [d1,d2,d3] >>> df = pd.DataFrame(li) >>> df A B C 0 1 2 3.0 1 a b NaN 2 (1, 2) ab 3.0
到此这篇关于pandas通过字典生成dataframe的方法步骤就介绍到这了。我不敢休息,因为我没有存款;我不敢说累,因为我没有成就;我不敢偷懒,因为我还要生活;我能放弃,但是我不能选择放弃。所以坚强、拼搏是我唯一的选择。更多相关pandas通过字典生成dataframe的方法步骤内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!