2024-04-25 16:57:11 发布
网友
当从字典在Pandas中创建数据帧时,元组会自动展开,即
import pandas d = {'a': 1, 'b': 2, 'c': (3,4)} df = pandas.DataFrame.from_dict(d) print(df)
退货
除了首先将元组转换为字符串之外,有没有任何方法可以防止这种情况发生?我希望结果是
a b c 0 1 2 (3, 4)
传递paramorient='index'并转置结果,使其不广播标量值:
orient='index'
In [13]: d = {'a': 1, 'b': 2, 'c': (3,4)} df = pd.DataFrame.from_dict(d, orient='index').T df Out[13]: a c b 0 1 (3, 4) 2
尝试添加[],因此带有c的dictionary中的值是tuple的list:
[]
c
dictionary
tuple
list
import pandas d = {'a': 1, 'b': 2, 'c': [(3,4)]} df = pandas.DataFrame.from_dict(d) print(df) a b c 0 1 2 (3, 4)
传递param
orient='index'
并转置结果,使其不广播标量值:尝试添加
[]
,因此带有c
的dictionary
中的值是tuple
的list
:相关问题 更多 >
编程相关推荐