0% found this document useful (0 votes)
5 views6 pages

Pandas and NumPy DataFrame Operations

The document contains multiple Python programs using Pandas and NumPy to perform various data manipulation tasks. These tasks include counting DataFrame columns, grouping data, checking column presence, counting array values based on conditions, finding occurrences of sequences in arrays, merging arrays, and combining elements from arrays of different shapes. Each task is accompanied by sample output and code snippets demonstrating the functionality.

Uploaded by

safe814866
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Pandas and NumPy DataFrame Operations

The document contains multiple Python programs using Pandas and NumPy to perform various data manipulation tasks. These tasks include counting DataFrame columns, grouping data, checking column presence, counting array values based on conditions, finding occurrences of sequences in arrays, merging arrays, and combining elements from arrays of different shapes. Each task is accompanied by sample output and code snippets demonstrating the functionality.

Uploaded by

safe814866
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. 14) Write a Pandas program to count number of columns of a DataFrame.

Sample Output:
0147
1258
2 3 6 12
3491
4 7 5 11
Number of columns:
3
import pandas as pd

# Sample DataFrame
data = {
'col1': [1, 2, 3, 4, 7],
'col2': [4, 5, 6, 9, 5],
'col3': [7, 8, 12, 1, 11]
}

df = [Link](data)

print("Original DataFrame:")
print(df)

# Count number of columns


num_columns = len([Link])

print("Number of columns:")
print(num_columns)

2. 15) Write a Pandas program to group by the first column and get
second column as lists inrows

Sample output
0 C1 1
1 C1 2
2 C2 3
3 C2 3
4 C2 4
5 C3 6
6 C2 5
Group on the col1:
col1
C1 [1, 2]
C2 [3, 3, 4, 5]
C3 [6]
Name: col2, dtype: object
import pandas as pd
# Sample DataFrame
data = {
'col1': ['C1', 'C1', 'C2', 'C2', 'C2', 'C3', 'C2'],
'col2': [1, 2, 3, 3, 4, 6, 5]
}

df = [Link](data)

print("Original DataFrame:")
print(df)

# Group by the first column and get second column as lists in rows
grouped = [Link]('col1')['col2'].apply(list)

print("Group on the col1:")


print(grouped)

3. 16) Write a Pandas program to check whether a given column is


present in a DataFrame or not. Sample data:
0147
1258
2 3 6 12
3491
4 7 5 11
Col4 is not
present in
DataFrame.
Col1 is
present in
DataFrame.

import pandas as pd

# Sample DataFrame
data = {
'col1': [1, 2, 3, 4, 7],
'col2': [4, 5, 6, 9, 5],
'col3': [7, 8, 12, 1, 11]
}

df = [Link](data)
# Function to check if column is present in DataFrame
def check_column_presence(column_name, dataframe):
if column_name in [Link]:
print(f"{column_name} is present in DataFrame.")
else:
print(f"{column_name} is not present in DataFrame.")

# Test the function


check_column_presence("Col4", df)
check_column_presence("col1", df)

4. 17) Create two arrays of six elements. Write a NumPy


program to count the number ofinstances of a value
occurring in one array on the condition of another array.
Sample Output:
Original arrays:
[ 10 -10 10 -10 -10 10]
[0.85 0.45 0.9 0.8 0.12 0.6 ]
Number of instances of a value occurring in one array on the
condition of another array:3

import numpy as np

# Create two arrays


array1 = [Link]([10, -10, 10, -10, -10, 10])
array2 = [Link]([0.85, 0.45, 0.9, 0.8, 0.12, 0.6])

print("Original arrays:")
print(array1)
print(array2)

# Count the number of instances of a value occurring in one array


based on the condition of another array
count = [Link]((array1 == 10) & (array2 > 0.5))

print("Number of instances of a value occurring in one array on the


condition of another array:", count)

5. 18) Create a 2-dimensional array of size 2 x 3, composed of 4-byte


integer elements. Write aNumPy program to find the number of
occurrences of a sequence in the said array. Sample Output:
Original NumPy array:
[[1 2 3]
[2 1 2]]
Type:
<class
'[Link]
array'>
Sequence
: 2,3
Number of occurrences of the said sequence: 2
import numpy as np

# Create a 2x3 NumPy array


array = [Link]([[1, 2, 3],
[2, 1, 2]], dtype=np.int32)

print("Original NumPy array:")


print(array)
print("Type:", type(array))

# Define the sequence to find


sequence = [2, 3]

# Convert the array and sequence to strings


array_str = str(array)
sequence_str = str(sequence)

# Count the occurrences of the sequence in the array string


count = array_str.count(sequence_str)

print("Sequence:", sequence)
print("Number of occurrences of the said sequence:", count)

19) Write a NumPy program to merge three given NumPy arrays of same shape

import numpy as np

# Create three NumPy arrays of the same shape

array1 = [Link]([[1, 2, 3],

[4, 5, 6]])

array2 = [Link]([[7, 8, 9],

[10, 11, 12]])

array3 = [Link]([[13, 14, 15],


[16, 17, 18]])

# Merge the arrays using concatenate

merged_array = [Link]((array1, array2, array3))

print("Merged array:")

print(merged_array)

1. 20) Write a NumPy program to combine last element with first


element of two given ndarraywith different shapes.

Sample Output:
Original arrays:
['PHP', 'JS', 'C++']
['P
yth
on',
'C#
',
'Nu
mP
y']
Aft
er
Co
mb
inin
g:
['PHP' 'JS' 'C++Python' 'C#' 'NumPy']

import numpy as np

# Define the original arrays


array1 = [Link](['PHP', 'JS', 'C++'])
array2 = [Link](['Python', 'C#', 'NumPy'])

print("Original arrays:")
print(array1)
print(array2)

# Combine the last element of the first array with the first element of the
second array
combined_array = [Link]((array1, array2))
print("After Combining:")
print(combined_array)

You might also like