0
data = read_csv('claims.csv', sep = ',')
groupby_gender = data.groupby('Gender')            
for gender, value in groupby_gender['Marker1']:
    print(gender, value)

This is what I tried to do to group the mentioned column but "Marker1" has two columns under it but when I run the program only one of the columns is displayed.

Gender  Age                 Marker1 
Female  Adult       CR213   381 385
Male    Adult       CR214   385 385
Male    Adult       CR215   385 385
Female  Adult       CR216   381 385

That is the table. Under the column "Marker1", there are those two numbers. So I am trying to read, say 381 and 385, together so I can compare them to another column.

Thatile
  • 5
  • 3

1 Answers1

0

As I understand it, the csv format does not support merged cells, so not sure if you have two columns with the same name, or a single column where each cell contains two items of data that you want to separate.

Assuming the former, you could try ignoring the headings in the csv file, and explicitly passing a list of columns to load and required headings (names).

e.g. If the columns you require are the first two in the csv file:

names = ['my_column_0', 'my_column_1']
data = read_csv('claims.csv', sep=',', usecols=[0,1], skiprows=[1], header=None, names=name)

If the latter, the answers here may help: How to split a column into two columns?

Violet
  • 311
  • 1
  • 4
  • 13
  • How would I read both columns if I read them directly from the Excel file? Because I am not trying to split them. I need to evaluate both columns, at the same time, row by row. – Thatile Sep 11 '18 at 12:21
  • If reading from an Excel workbook (xls or xlsx suffix rather than csv), you should use [pandas.read_excel](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html), instead of pandas.read_csv. If the problem is a merged cell used as a heading, you can use the approach described above i.e. specify the columns you want, skip the first row (assuming that's where the merged cell is), supply a list of column names to use. – Violet Sep 24 '18 at 13:19