Q.Kavyanjali, a chemical analyst, needs to arrange data of few elements in the form of two series containing symbols and their atomic numbers respectively. Thereafter, the data of these two series has to be arranged and displayed in the form of Data Frame as shown below: Symbol Atomic Number Hydrogen H 1 Helium He 2 Lithium Li 3 Beryllium Be 4 Help her in writing suitable Python code to complete the task.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Kavyanjali needs to create a pandas DataFrame with two columns—element symbols and atomic numbers—by first storing the data in separate lists, then combining them into a dictionary, and finally converting that dictionary into a DataFrame for display.
The task at hand is straightforward but foundational: organizing chemical data into a structured table that Python can manipulate and display. Kavyanjali has raw information—element names, their symbols, and atomic numbers—and needs to transform it into a DataFrame, which is essentially a two-dimensional labeled data structure, much like a spreadsheet or SQL table. The pandas library in Python is the standard tool for this job.
She begins by recognizing that the data naturally falls into two parallel sequences: one holding the chemical symbols (H, He, Li, Be) and the other holding the corresponding atomic numbers (1, 2, 3, 4). The most intuitive approach is to store these as two Python lists. Once the lists are ready, she can pair them together—symbols as one column, atomic numbers as another—and feed them into a DataFrame constructor.
The pandas library offers multiple ways to build a DataFrame, but the cleanest method here is to use a dictionary. Each key in the dictionary becomes a column name, and the associated list becomes the column's data. The DataFrame constructor then reads this dictionary and arranges everything into rows and columns, automatically aligning the data by position.
Here is the Python code that accomplishes this:
import pandas as pd
# Step 1: Create two lists for symbols and atomic numbers
symbols = ['H', 'He', 'Li', 'Be']
atomic_numbers = [1, 2, 3, 4]
# Step 2: Combine them into a dictionary
data = {
'Symbol': symbols,
'Atomic Number': atomic_numbers
}
# Step 3: Create the DataFrame
df = pd.DataFrame(data)
# Step 4: Display the DataFrame
print(df)
When this code runs, the output will be:
Symbol Atomic Number
0 H 1
1 He 2
2 Li 3
3 Be 4
``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.