嗨,我写了一些代码来建立一个默认字典
def makedata(filename):
with open(filename, "r") as file:
for x in features:
previous = []
count = 0
for line in file:
var_name = x
regexp = re.compile(var_name + r'.*?([0-9.-]+)')
match = regexp.search(line)
if match and (match.group(1)) != previous:
previous = match.group(1)
count += 1
if count > wlength:
count = 1
target = str(str(count) + x)
dict.setdefault(target, []).append(match.group(1))
file.seek(0)
df = pd.DataFrame.from_dict(dict)
字典看起来不错,但当我试图转换为dataframe时,它是空的。我想不通
口述:
{'1meanSignalLenght': ['0.5305184', '0.48961428', '0.47203177', '0.5177274'], '1amplCor': ['0.8780955002105448', '0.8634431017504487', '0.9381169983046714', '0.9407036427333355'], '1metr10.angle1': ['0.6439386643584522', '0.6555194964997434', '0.9512436169922103', '0.23789348400794422'], '1syncVar': ['0.1344131181025432', '0.08194580887223515', '0.15922251165913678', '0.28795644612520327'], '1linVelMagn': ['0.07062673289287498', '0.08792496681784517', '0.12603999663935528', '0.14791253129369603'], '1metr6.velSum': ['0.17850601560734558', '0.15855169971072014', '0.21396496345720045', '0.2739525279330513']}
数据框:
^{pr2}$
这应该从字典中生成一个数据帧。在
您可以简单地使用
pd.DataFrame(list_of_dicts)
(如果变量不是列表,则使用pd.DataFrame([dict])
)传递dict列表,或者使用pd.DataFrame.from_dict(dict)
传递列表dict。在最后一个例子中,dict应该是dict = {a:[1,2,3], "b": ["a", "b", "c"], "c":...}
。在参见:Pandas Dataframe from dict with empty list value
我认为你的部分问题是你使用了关键字dict,假设它是一个变量
在你的函数中创建一个字典,把它叫做dict以外的东西。让你的函数返回字典。然后,当您生成一个数据帧时,使用该返回值。现在,您正在从一个空的dictionary对象创建一个数据帧。在
相关问题 更多 >
编程相关推荐