2

This is the <class 'pandas.core.frame.DataFrame'> from my csv file. This is because i want to group all Header A that has "happy" value in Header C

print(df.loc[df["Header C"] == "happy"])

and this is the output:

    Header A    Header B    Header C   Header D    Header E
0     rowa.a      rowa.c       happy     rowa.d      rowa.e
3     rowc.a      rowc.c       happy     rowd.d      rowc.e
5     rowe.a      rowe.c       happy     rowe.d      rowe.e

Is there any way i can make the the output Header's into a list and store it in a variable ?

same with the rows, i also want it to make a list and store it in a variable.

(like this):

a = ["Header A", "Header B", "Header C", "Header D", "Header E"]
n = ["rowa.a", "rowa.c", "happy", "rowa.d", "rowa.e"]
o = ["rowc.a", "rowc.c", "happy", "rowc.d", "rowc.e"]

What i want to be as a final output is a prettytable

from prettytable import PrettyTable
x = PrettyTable()
x.field_names = a
x.add_row(n)
x.add_row(o)
print(x)

and an output of this:

+----------+----------+----------+----------+----------+
| Header A | Header B | Header C | Header D | Header E |
+----------+----------+----------+----------+----------+
|  rowa.a  |  rowa.c  |  happy   |  rowa.d  |  rowa.e  |
+----------+----------+----------+----------+----------+

Thank you very much!

4 Answers4

3

Hey you can convert your data frame back to the dictionary and then access all lists you have described:

df.to_dict('split')
Ihor Shylo
  • 572
  • 5
  • 12
0

You can do:

# get header and df values
all_rows = [df.columns.tolist()] + df.to_numpy().tolist()

# get each rows (or you can loop through all_rows)
a = all_rows[0]
n = all_rows[1]
o = all_rows[1]
YOLO
  • 20,181
  • 5
  • 20
  • 40
0

There are a couple different ways to solve your problem.

I'd probably go with this solution:

Pandas offers iterators over the DataFrame class to access a dataframe e.g. row-by-row. DataFrame.iteritems() should do the trick.

It iterates over your dataframe and gives you tuples of (column_name, values) where column_name is type str and values is type Series. To return the values of a Series as a list, you can use Series.tolist()

Here's a rough sketch of a possible solution:

import pandas as pd


df = pd.DataFrame({
    "Header A:" : ["foo_a","bar_a","baz_a"],
    "Header B:" : ["foo_b","bar_b","baz_b"],
    "Header C:" : ["foo_c","bar_c","baz_c"]
})


for column,values in df.iteritems():
    print(column)
    print(values.tolist)
PStein
  • 38
  • 5
0
from prettytable import PrettyTable
import pandas as pd

# create dataframe
df = pd.DataFrame({
    'a': ["Header A", "Header B", "Header C", "Header D", "Header E"],
    'n': ["rowa.a", "rowa.c", "happy", "rowa.d", "rowa.e"],
    'o': ["rowc.a", "rowc.c", "happy", "rowc.d", "rowc.e"]
})

# create pretty table
table = PrettyTable()
table.field_names = df['a'].tolist()
table.add_row(df['n'].tolist())
table.add_row(df['o'].tolist())

print(x)

Alternatively when you have many columns and don't want to list them manually, iteration can be used:

table = PrettyTable()
table.field_names = df[next(iter(df.columns))].tolist()
for col in df.columns:
  table.add_row(df[col].tolist())
Alexandra Dudkina
  • 4,302
  • 3
  • 15
  • 27