Create dataframe pandas by dicionario 1 key and 1 value

Asked

Viewed 323 times

-3

How do I create a Data Frame Pandas through a dictionary counting only 1 key and 1 value

dic = {'banco': 0,
 'correndo': 1,
 'da': 2,
 'de': 3,
 'depois': 4,
 'descasnando': 5,
 'foi': 6,
 'ladrão': 7,
 'no': 8,
 'praça': 9,
 'roubando': 10,
 'roubar': 11,
 'saiu': 12,
 'um': 13,
 'visto': 14}

When I do pd.DataFrame(dic).T it creates the data frame being the indexes the words and not 0 to 14.

Expected Departure:

0   banco        0
1   correndo     1
2   da           2
3   de           3
4   depois       4
5   descasnando  5
6   foi          6
7   ladrão       7
8   no           8
9   praça        9
10  roubando    10
11  roubar      11
12  saiu        12
13  um          13
14  visto       14
  • Please say how you want the output to be

  • good question )

  • these values that are in the dictionary are also part of the dataframe )

  • The answer below does not satisfy what you want?

1 answer

0


If you want to use the values as index and the words as values you can use the following code:

import pandas as pd
pd.DataFrame(list(dic.keys()), index = dic.values())

In the first parameter we pass what will be the values of the columns and in the second parameter we pass the indexes.

Entree:

dic = {'banco': 0,
 'correndo': 1,
 'da': 2,
 'de': 3,
 'depois': 4,
 'descasnando': 5,
 'foi': 6,
 'ladrão': 7,
 'no': 8,
 'praça': 9,
 'roubando': 10,
 'roubar': 11,
 'saiu': 12,
 'um': 13,
 'visto': 14,
 'teste': 22} 

Exit:

    0   banco
    1   correndo
    2   da
    3   de
    4   depois
    5   descasnando
    6   foi
    7   ladrão
    8   no
    9   praça
    10  roubando
    11  roubar
    12  saiu
    13  um
    14  visto
    22  teste

If you want words like Dice and values in columns just invert:

pd.DataFrame(list(dic.values()), index = dic.keys())

Update

pd.DataFrame(dic.items(), index = dic.values())

That way the output will be index, word, value

Exit:

0   banco        0
1   correndo     1
2   da           2
3   de           3
4   depois       4
5   descasnando  5
6   foi          6
7   ladrão       7
8   no           8
9   praça        9
10  roubando    10
11  roubar      11
12  saiu        12
13  um          13
14  visto       14
22  teste       22

To leave the content to the pandas can do only:

pd.DataFrame(dic.items())
  • Blz worked here dic.items() tip was perfect!!!

  • Naokity, good night! If the answer is what you were looking for, consider marking as accepted. This helps people visualize that the question has already been solved and also encourages others to answer their questions. See how Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.