Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

python 3.x - From list of list of dictionaries to Pandas DataFrame

I've the following list format (see code snippet) and I want to create a DataFrame to achieve the desired result.

Desired result:8.00 4.84 1.416 2.56 3.104 3.09 1.184 7.50 15.00

```
data = [[ {'C': 8, 'G': 1, 'T': 1},
          {'C': 4.84, 'G': 1, 'T': 2},
          {'C': 1.416, 'G': 1, 'T': 3}],
         [{'C': 2.56, 'G': 1, 'T': 1},
          {'C': 3.104, 'G': 1, 'T': 2},
          {'C': 3.09, 'G': 1, 'T': 3}],
         [{'C': 1.184, 'G': 1, 'T': 1},
          {'C': 7.5, 'G': 1, 'T': 2},
          {'C': 15, 'G': 1, 'T': 3}]]
```

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looking at your data, it looks like you have a list of list of dictionaries. So it just need to be flattened and loaded as a dataframe. Once you have the data in the dataframe, you need to convert the float into 3 decimal place string format, join the list of values and print.

Here's how I will do it:

  • Step 1: Flatten the data to a normal dictionary with key:value pair
  • Step 2: Load the key:value pair dictionary into a dataframe

Steps 1 & 2 are accomplished using this list comprehension + DataFrame creation step

df = pd.DataFrame([k for klist in data for k in klist])
  • Step 3: Convert the C column into a string format with 3 decimal places
  • Step 4: Concatenate the list as a string using .join() while adding ' ' as separator

Steps 3 & 4 are accomplished using this single line map and join function.

c_list = '
'.join(df.C.map('{:,.3f}'.format).tolist())
  • Step 5: print the data in raw format to get the as well.

Step 5 is just to print and is another line. I am using repr to give you the data on the same line.

print (repr(c_list))

You can do it as follows:

data = [[ {'C': 8, 'G': 1, 'T': 1},
          {'C': 4.84, 'G': 1, 'T': 2},
          {'C': 1.416, 'G': 1, 'T': 3}],
         [{'C': 2.56, 'G': 1, 'T': 1},
          {'C': 3.104, 'G': 1, 'T': 2},
          {'C': 3.09, 'G': 1, 'T': 3}],
         [{'C': 1.184, 'G': 1, 'T': 1},
          {'C': 7.5, 'G': 1, 'T': 2},
          {'C': 15, 'G': 1, 'T': 3}]]
import pandas as pd
df = pd.DataFrame([k for klist in data for k in klist])
c_list = '
'.join(df.C.map('{:,.3f}'.format).tolist())
print (repr(c_list))

The output of this will be:

'8.000
4.840
1.416
2.560
3.104
3.090
1.184
7.500
15.000'

To print 3 items one each line, you can do the following:

for i in range(0,len(c_list),3):
    print(repr('
'.join(c_list[i:i+3])))

or you can try to print it as:

for i in range(0,len(c_list),3):
    print(r'
'.join(c_list[i:i+3]))

The output will be:

'8.000
4.840
1.416'
'2.560
3.104
3.090'
'1.184
7.500
15.000'

I assume you are asking for this.

I added an extra line to the input dictionary {'C': 12.5, 'G': 2, 'T': 8}

The output is as follows:

'8.000
4.840
1.416'
'2.560
3.104
3.090'
'1.184
7.500
15.000'
'12.500'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...