Home > Blockchain >  extracted data from sql for processing using python
extracted data from sql for processing using python

Time:12-24

I have saved out a data column as follows:

[[A,1], [B,5], [C,18]....]

i was hoping to group A,B,C as shown above into Category and 1,5,18 into Values/Series for updating of my powerpoint chart using python pptx. Example:

Category Values
A 1
B 5

Is there any way i can do it? currently the above example is also extracted as strings so i believe i have to convert it to lists first?

thanks in advance!

CodePudding user response:

Try to parse your strings (a list of lists) then create your dataframe from the real list:

import pandas as pd
import re

s = '[[A,1], [B,5], [C,18]]'

cols = ['Category', 'Values']
data = [row.split(',') for row in re.findall('\[([^]] )\]', s[1:-1])]
df = pd.DataFrame(data, columns=cols)
print(df)

# Output:
  Category Values
0        A      1
1        B      5
2        C     18

CodePudding user response:

You should be able to just use pandas.DataFrame and pass in your data, unless I'm misunderstanding the question. Anyway, try:

df = pandas.DataFrame(data=d, columns = ['Category', 'Value'])

where d is your list of tuples.

CodePudding user response:

from prettytable import PrettyTable

column = [["A",1],["B",5],["C",18]]

columnname=[]
columnvalue =[]
t = PrettyTable(['Category', 'Values'])

for data in column:
    columnname.append(data[0])
    columnvalue.append(data[1])
    t.add_row([data[0], data[1]])
    
print(t)
  • Related