Home »
Python »
Python Programs
How to convert a pandas DataFrame subset of columns AND rows into a numpy array?
Given a pandas dataframe, we have to convert a pandas DataFrame subset of columns AND rows into a numpy array.
By Pranit Sharma Last updated : October 03, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Columns are the different fields that contain their particular values when we create a DataFrame. We can perform certain operations on both row & column values.
Problem statement
Suppose we are given a DataFrame containing some columns like a, b, c, and d and we need to select only those rows in which the value for column c is greater than 1, but we only need columns of a and b for these rows.
Converting a pandas DataFrame subset of columns AND rows into a numpy array
For this purpose, we are simply going to filter out the columns of DataFrame with the traditional square bracket fashion inside which we will write a specific condition according to our requirements. Consider the below-given code statement to achieve this task,
res = df[df.c > 1][['a', 'b']].values
Let us understand with the help of an example,
Python program to convert a pandas DataFrame subset of columns AND rows into a numpy array
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating two dictionary
d = {
'a':[0.9,0.8,1,1.1,0],
'b':[0.3,0.5,2.1,1,1.2],
'c':[0,0,1.1,1.9,0.1],
'd':[9,8,0,0,0]
}
# Creating a DataFrame
df = pd.DataFrame(d)
# Display DataFrame
print("DataFrame1:\n",df,"\n")
# Filtering out data
res = df[df.c > 1][['a', 'b']].values
# display result
print("Result:\n",res)
Output
The output of the above program is:
Python Pandas Programs »