首页 > 解决方案 > 修改字典中的数据框

问题描述

我有一个像这样的数据框字典:

variables = [x, y, z, t]
dictionary_of_dataframes = {'df1':pd.DataFrame(data1, columns =variables),'df2' : pd.DataFrame(data2, columns =variables),'df3' : pd.DataFrame(data3, columns =variables)}

我正在尝试将存储在变量中的一些数据转换为数字。

for elem in dictionary_of_logs:
    elem[['x','y']].apply(pd.to_numeric)

但它返回我TypeError: string indices must be integers。有可能这样循环吗?

标签: pythonpandas

解决方案


如果你在 dict 中使用 for,你只会得到键。如果要迭代值,请使用:

for elem in dictionary_of_logs.values():
    elem[['x','y']].apply(pd.to_numeric)

推荐阅读