我有一个与嵌套字典创建的Pandas DataFrame相关的问题。
我注意到当我使用以下dict创建数据帧时,单元格都是NaN
dict1 = {
0: {('kfield',): 100.0},
1: {('kfield',): 101.0},
2: {('kfield',): 102.5},
3: {('kfield',): 103.5},
4: {('kfield',): 104.5},
5: {('kfield',): 105.5}
}
df = pd.DataFrame(dict1)
print df
结果
0 1 2 3 4 5
kfield NaN NaN NaN NaN NaN NaN
但是,当内部字典键由具有多个值的元组组成时,将使用适当的值创建数据帧。
dict2 = {
0: {('kfield','kfield2',): 100.0},
1: {('kfield','kfield2',): 101.0},
2: {('kfield','kfield2',): 102.5},
3: {('kfield','kfield2',): 103.5},
4: {('kfield','kfield2',): 104.5},
5: {('kfield','kfield2',): 105.5}
}
df2 = pd.DataFrame(dict2)
print df2
结果
0 1 2 3 4 5
kfield kfield2 100.0 101.0 102.5 103.5 104.5 105.5
不确定Pandas为什么会这样。我错过了什么吗?
我正在使用Pandas版本'0.19.2'
答案 0 :(得分:0)
看起来这个bug与旧的Pandas版本有关。我已经验证了代码与Python3和Pandas v0.22.0
一样正常工作答案 1 :(得分:0)
如果您删除了最后一个','来自元组' dict1'字典然后按预期工作。提供相同的代码。
import pandas as pd
dict1 = {
0: {('kfield'): 100.0},
1: {('kfield'): 101.0},
2: {('kfield'): 102.5},
}
dict2 = {
0: {('kfield','kfield2',): 100.0},
1: {('kfield','kfield2',): 101.0},
2: {('kfield','kfield2',): 102.5},
}
df2 = pd.DataFrame(dict2)
df = pd.DataFrame(dict1)
print df
print df2
Output:
0 1 2
kfield 100.0 101.0 102.5
0 1 2
kfield kfield2 100.0 101.0 102.5