ALL BUSINESS DATA ANALYSIS PYTHON

How to select rows and columns in pandas (Python)

Πως να επιλέγετε γραμμές και στήλες με τη βιβλιοθήκη pandas της γλώσσας προγραμματισμού Python

See below the data and the code for the result shown on image . . .

How to select rows and columns in pandas

# Credit : Trifonas Papadopoulos, Oct 4th, 2022, Version 2.0

# start of code —————————————————————————————–

 

import pandas as pd

import numpy as np

 

# data

data = [[‘A0001′,”,”,”,0,’variable’,0,0],

[‘A0001′,’WHITE’,’S’,’Small’,1,’variation’,0,0],

[‘A0001′,’WHITE’,’M’,’Medium’,2,’variation’,0,0],

[‘A0001′,’WHITE’,’L’,’Large’,3,’variation’,0,0],

[‘A0001′,’WHITE’,’XL’,’Extra Large’,4,’variation’,0,0],

[‘A0001′,’BLACK’,’L’,’Large’,5,’variation’,0,0],

[‘A0003′,”,”,”,6,’variable’,0,0],

[‘A0003′,’BLACK’,’XL’,’Extra Large’,7,’variation’,0,0],

[‘A0002′,”,”,”,8,’variable’,0,0],

[‘A0002′,’BLACK’,’L’,’Large’,9,’variation’,0,0],

[‘A0002′,’BLACK’,’XL’,’Extra Large’,10,’variation’,0,0],

[‘A0004′,”,”,”,12,’variable’,0,0],

[‘A0004′,’WHITE’,’S’,’Small’,12,’variation’,0,0],

[‘A0004′,’RED’,’S’,’Small’,13,’variation’,0,0],

[‘A0004′,’RED’,’M’,’Medium’,14,’variation’,0,0],

[‘A0001′,’WHITE’,’XXL’,’2 Extra Large’,15,’variation’,0,0],

]

 

# print list of columns

print(‘–list of columns———————————————————–‘)

cols = list(df.columns.values)

print(cols)

 

# dataframe with defined column names

df = pd.DataFrame(data, columns=[‘parent’, ‘color’, ‘size’, ‘size_desc’, ‘order’, ‘type’, ‘quantity’,’price’])

print(”)

print(‘–dataframe with defined column names—————————————‘)

print(df)

# end of code —————————————————————————————–

If you want to see more ways to select rows and columns in pandas (Python), read my code (Version 2) in  html format.

Views: 13

Comments are closed.

Pin It