python - pandas.dataframe 到orderedDictionary : using a passed argument to specify the key column name instead of explicitly writing it

标签 python pandas dictionary dataframe ordereddictionary

基于此answer我想编写一个函数将 csv 加载到 OrderedDict() 中,但我不知道如何解决将键列名称作为字符串传递而不是手动说明的问题它?这是我的代码,可以让它更清楚:

dic_key = 'uniqueID'
df.dic_key #this gives AttributeError: 'DataFrame' object has no attribute 'dic_key'

而不是df.uniqueID,其中uniqueID是我们想要将其用作键的列的名称

完整代码如下:

def csv_to_OrderedDic1(path, dic_key='uniqueID'):
    '''

    Parameters:
        dic_key: the name of the column to be used as the dictionary key

    '''
    df = pd.DataFrame.from_csv(path, sep='\t', header=0)
    # Get an unordered dictionary
    unordered_dict = df.set_index(dic_key).T.to_dict('list')
    # Then order it
    ordered_dict = OrderedDict((k,unordered_dict.get(k)) for k in df.dic_key)
    return ordered_dict

最佳答案

我认为更好的是使用 read_csv对于选择列 [] 而不是点符号:

def csv_to_OrderedDic1(path, dic_key='uniqueID'):
    '''

    Parameters:
        dic_key: the name of the column to be used as the dictionary key

    '''
    df = pd.read_csv(path, sep='\t', header=0)
    # Get an unordered dictionary
    unordered_dict = df.set_index(dic_key).T.to_dict('list')
    # Then order it
    ordered_dict = OrderedDict((k,unordered_dict.get(k)) for k in df[dic_key])
    return ordered_dict

另一种解决方案,使用zip并通过 drop 删除列:

def csv_to_OrderedDic1(path, dic_key='uniqueID'):
    '''

    Parameters:
        dic_key: the name of the column to be used as the dictionary key

    '''
    df = pd.read_csv(path, sep='\t', header=0)
    L = zip(df[dic_key], df.drop(dic_key, 1).values.tolist())
    ordered_dict = OrderedDict(L)
    return ordered_dict

关于python - pandas.dataframe 到orderedDictionary : using a passed argument to specify the key column name instead of explicitly writing it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46644248/

相关文章:

python - 如何将 WSGI 与 Python/Flask 应用程序一起使用 - HasGeek Lastuser?

python - SQL 查找非零的最小值

python - 从 2 个浮点列创建日期时间列

ios - MapBox:如何删除一个形状并绘制另一个形状?

python - pandas:合并帮助两个数据框

python - 检查索引处 numpy 数组的值

python - 如何在pandas python中查找和匹配不同数据框中的特定值

python - 塑造神经网络分类输出维度?

java - 按值对 Map<Key, Value> 进行排序

python - 创建Python字典: Syntax Problems