By R.A.Munna


2019-09-11 09:12:19 8 Comments

I have dictionary like

my_dict={
"A": [["value-1", "value-2"], [1, 2]],
"B": [["price-1", "price-2"], [1,2]]
}

I want to dump this dictionary with pandas into csv file

import pandas as pd
df = pd.DataFrame.from_dict(data=my_dict, orient='index')
df.to_csv("data/mydict_11sept.csv", sep=',')

output:

           A           |       B
________________________________________________    
["value-1", "value-2"] | ["price-1", "price-2"]
       [1, 2]          |     [1,2]

expected Output:

    A       |    B
-----------------------
value-1 | 1 | price-1 | 1
value-2 | 2 | price-2 | 2

Updated: output of my file is given and expected file format is given

enter image description here

1 comments

@jezrael 2019-09-11 09:17:43

Use preprocessing first in dict comprehension with enumerate:

d = {f'{k}_{i}': x for k, v in my_dict.items() for i, x in enumerate(v)}
df = pd.DataFrame(d)
print (df)
       A_0  A_1      B_0  B_1
0  value-1    1  price-1    1
1  value-2    2  price-2    2

EDIT:

d = {k:list(zip(*v)) for k, v in my_dict.items()}
df = pd.DataFrame(data=d)
print (df)
              A             B
0  (value-1, 1)  (price-1, 1)
1  (value-2, 2)  (price-2, 2)

Or:

d = {k:[', '.join(str(y) for y in x) for x in zip(*v)] for k, v in my_dict.items()}
df = pd.DataFrame(data=d)
print (df)
            A           B
0  value-1, 1  price-1, 1
1  value-2, 2  price-2, 2

EDIT1: Here is possible create MultiIndex in columns:

d = {(k, i) : x for k, v in my_dict.items() for i, x in enumerate(v)}
df = pd.DataFrame(d)
print (df)
         A           B   
         0  1        0  1
0  value-1  1  price-1  1
1  value-2  2  price-2  2

And if necessary, remove second level, but not recommended, because problem selectin by columns names only one column:

df.columns = df.columns.droplevel(1)
print (df)
         A  A        B  B
0  value-1  1  price-1  1
1  value-2  2  price-2  2

print (df['A'])
         A  A
0  value-1  1
1  value-2  2

@R.A.Munna 2019-09-11 09:41:29

Thanks. Is there any way to merge?

@jezrael 2019-09-11 09:44:02

@R.A.Munna - Can you explain more?

@R.A.Munna 2019-09-11 09:45:48

In the question first row is merge with corresponding list. Thank you.

@jezrael 2019-09-11 10:09:04

@R.A.Munna - added 2 new solutions - first for tuples, second for joined values to strings

@R.A.Munna 2019-09-11 10:37:01

it's work but I have wanted about the firs row merge and the corresponding list will be in separated column. But your output is in the same column touple or string. Please look I have updated the question with formatting. Thank you.

@jezrael 2019-09-11 11:16:05

@R.A.Munna - Answer was edited.

@R.A.Munna 2019-09-12 02:33:17

thank you, Finally that's the thing is manageable :)

@jezrael 2019-09-12 04:02:10

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] How to remove a key from a Python dictionary?

15 Answered Questions

[SOLVED] How to clone or copy a list?

44 Answered Questions

[SOLVED] How to make a flat list out of list of lists

22 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3527050 View
  • 3474 Score
  • 22 Answer
  • Tags:   python directory

9 Answered Questions

[SOLVED] Writing a pandas DataFrame to CSV file

28 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2406860 View
  • 3235 Score
  • 28 Answer
  • Tags:   python list

14 Answered Questions

[SOLVED] "Large data" work flows using pandas

17 Answered Questions

[SOLVED] How to iterate over rows in a DataFrame in Pandas?

16 Answered Questions

25 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

Sponsored Content