相关疑难解决方法(0)

python pandas dataframe to dictionary

我有一个两列数据帧,并打算将其转换为python字典 - 第一列将是键,第二列将是值.先感谢您.

数据帧:

    id    value
0    0     10.2
1    1      5.7
2    2      7.4
Run Code Online (Sandbox Code Playgroud)

python dictionary pandas

86
推荐指数
8
解决办法
13万
查看次数

dataframe to dict 使得一列是键,另一列是值

我有数据框

    ID   A   B   C
0   p    1   3   2
1   q    4   3   2
2   r    4   0   9  
Run Code Online (Sandbox Code Playgroud)

我想创建一个字典,其中 ID 是键,B 是值,所以它将是:

d["q"] = 3 , d["r"] = 0

这样做的最佳方法是什么?

它与假设的重复不同,因为我想要每个键的单个值而不是列表

python dictionary dataframe pandas

11
推荐指数
2
解决办法
1万
查看次数

将哈希值分配给pandas中的分类数据行

所以我有许多带有3列分类变量的pandas数据框:

             D              F     False
             T              F     False
             D              F     False
             T              F     False
Run Code Online (Sandbox Code Playgroud)

第一列和第二列可以采用三个值中的一个.第三个是二进制.因此总共有18个可能的行(并非所有组合都可以在每个数据帧上表示).

我想为每一行分配一个数字1-18,以便具有相同组合因子的行被赋予相同的数字,反之亦然(没有哈希冲突).

在熊猫中最有效的方法是什么?

所以,all_combination_df是一个df与所有可能的因素组合.我试图将df转换为big_df 具有唯一数字的系列

import pandas, itertools

def expand_grid(data_dict):
    """Create a dataframe from every combination of given values."""
    rows = itertools.product(*data_dict.values())
    return pandas.DataFrame.from_records(rows, columns=data_dict.keys())

all_combination_df = expand_grid(
                           {'variable_1': ['D', 'A', 'T'],
                           'variable_2': ['C', 'A', 'B'],
                           'variable_3'     : [True, False]})

big_df = pandas.concat([all_combination_df, all_combination_df, all_combination_df])
Run Code Online (Sandbox Code Playgroud)

python hash dataframe pandas

5
推荐指数
1
解决办法
594
查看次数

将pandas数据帧转换为字典

我有一个pandas数据帧如下:

df=pd.DataFrame({'a':['red','yellow','blue'], 'b':[0,0,1], 'c':[0,1,0], 'd':[1,0,0]})
df
Run Code Online (Sandbox Code Playgroud)

看起来像

    a       b   c   d
0   red     0   0   1
1   yellow  0   1   0
2   blue    1   0   0
Run Code Online (Sandbox Code Playgroud)

我想将它转换为字典,以便我得到:

red     d
yellow  c
blue    b
Run Code Online (Sandbox Code Playgroud)

如果数据集非常大,请避免使用任何迭代方法.我还没有找到解决方案.任何帮助表示赞赏.

python dictionary dataframe python-3.x pandas

4
推荐指数
1
解决办法
3200
查看次数

将 Pandas 转换为定义用于键值的列的字典

有熊猫数据框“test_df”。我的目标是将其转换为字典。因此我运行这个:

   id   Name   Gender  Age  
0  1  'Peter'   'M'     32    
1  2  'Lara'    'F'     45   
Run Code Online (Sandbox Code Playgroud)

因此我运行这个:

test_dict = test_df.set_index('id').T.to_dict()
Run Code Online (Sandbox Code Playgroud)

输出是这样的:

{1: {'Name': 'Peter', 'Gender': 'M', 'Age': 32}, 2: {'Name': 'Lara', 'Gender': 'F', 'Age': 45}}
Run Code Online (Sandbox Code Playgroud)

现在,我只想选择“名称”和“性别”列作为字典键的值。我正在尝试将上面的脚本修改成这样:

test_dict = test_df.set_index('id')['Name']['Gender'].T.to_dict()
Run Code Online (Sandbox Code Playgroud)

没有成功!

请问有什么建议吗?!

dictionary pandas

3
推荐指数
1
解决办法
2543
查看次数

将pandas数据帧转换为字典,其中键是索引,值是列值列表

我有一个数据帧

DF:

    cola    colb   colc   cold
0      0    'a'     'b'   'c'
1      1    'd'     None  None
2      2    'g'     'h'   None
Run Code Online (Sandbox Code Playgroud)

我想将其转换为dict索引是键的位置,列值列表是如下的值:

d = {0 : [0,'a','b','c'], 1: [1,'d'], 2: [2,'g','h'] }
Run Code Online (Sandbox Code Playgroud)

我尝试了什么:

df.to_dict(orient='index')
Run Code Online (Sandbox Code Playgroud)

我也试过orient参数中的其他值,但没有任何效果.

编辑:

我想忽略字典中的NULL值,如输出中所示.

python python-3.x pandas

3
推荐指数
1
解决办法
120
查看次数

使用pandas将csv文件作为字典读取

我有这个人.以第一行为标题的csv:

A      B
test    23
try     34
Run Code Online (Sandbox Code Playgroud)

我想在这里读作字典,所以这样做:

dt = pandas.read_csv('file.csv').to_dict()
Run Code Online (Sandbox Code Playgroud)

但是,这会将标题行作为键读入.我希望列'A'中的值是键.我该怎么做,即得到这样的答案:

{'test':'23', 'try':'34'}
Run Code Online (Sandbox Code Playgroud)

python dictionary pandas

2
推荐指数
2
解决办法
9107
查看次数

如何将 DataFrame 反转回其代码形式?

我希望能够将数据帧反转回其代码形式。

所以我们知道这个代码形式

dat=pd.DataFrame(dict(Date=['19/11/2012', '20/11/2012', '21/11/2012'],
                    A=[1,3,1],
                    B=[3,2,2],
                    C=[4,3,2],
                    D=[5,2,2],))
Run Code Online (Sandbox Code Playgroud)

在创建的DataFrame 中给出结果,如下所示:

+---+------------+---+---+---+---+
|   |    Date    | A | B | C | D |
+---+------------+---+---+---+---+
| 0 | 19/11/2012 | 1 | 3 | 4 | 5 |
| 1 | 20/11/2012 | 3 | 2 | 3 | 2 |
| 2 | 21/11/2012 | 1 | 2 | 2 | 2 |
+---+------------+---+---+---+---+
Run Code Online (Sandbox Code Playgroud)

但是,有没有办法让我将上面的DataFrame转换回其代码形式?在上面的示例中,我事先知道创建 DataFrame 的代码,但实际上我没有这些信息。

python reverse-engineering dataframe pandas

2
推荐指数
1
解决办法
143
查看次数