Start Writing Python: A Guide for the Rookie "Create Bar Graph" 🐍 EP.3
Reading data from CSV and creating a bar graph with a simple Python
To work with data, we often need to manage CSV-type files that store data in a tabular format, such as user lists, sales reports, or demographics. In this article, we will learn how to:
• Read data from CSV files
• Display information on the screen
• Draw Bar Chart with each person's name and age
All of this uses Python in conjunction with popular libraries like pandas and matplotlib.
🔧 Sample code
Import pandas as pd.
Import matplotlib.pyplot as plt
# Read CSV file by full path
file _ path = '/ media / acdc / 473f8881-2823-4afa-aca5-e819a648674e / D1.csv'
df = pd.read _ csv (file _ page)
# Print all data
Print (df)
# Print each row
For index, row in df.iterrows ():
Print (f "Name: {row ['Name']}, Age: {row ['Age']}, Gender: {row ['Gender']}")
# Plot bar chart of Age by name
plt.figure (figsize = (10,6))
plt.bar (df ['Name'], df ['Age'], color = 'skyblue')
plt.xlabel ('name')
plt.ylabel ('Age')
plt.title ('Age of Each Person')
plt.xticks (rotation = 45)
plt. Tight _ layout ()
plt.show ()
📖 Describe the code part by part.
📦 1.Import library
Import pandas as pd.
Import matplotlib.pyplot as plt
• Pandas are used for managing table data and CSV
• Matplotlib.pyplot is used to create graphs such as bar, line and circle graphs.
📄 2. Read the data from the CSV file.
file _ path = '/ media / acdc /.../ D1.csv'
df = pd.read _ csv (file _ page)
• We use read _ csv () to load the data file.
• The df variable becomes a DataFrame data table.
📤 3. Show all the information.
Print (df)
• Use to view all data in the loaded CSV file
🔁 4. Show row-by-row data.
For index, row in df.iterrows ():
Print (f "Name: {row ['Name']}, Age: {row ['Age']}, Gender: {row ['Gender']}")
• Loop by row to display the information by name. Explicit description
• Suitable for checking data one by one
📊 5. Create a bar graph.
plt.figure (figsize = (10,6))
plt.bar (df ['Name'], df ['Age'], color = 'skyblue')
• Create a 10x6 inch graph
• df ['name'] is used as the X-axis
• df ['Age'] is used as the height of the stick.
🏷️ 6. Decorate the graph to look good.
plt.xlabel ('name')
plt.ylabel ('Age')
plt.title ('Age of Each Person')
plt.xticks (rotation = 45)
plt. Tight _ layout ()
• Enter the names of the X and Y axes
• Name the graph
• Rotate the X-axis name to tilt slightly for easy reading.
• Tight _ layout () keeps the graph from cutting edges
👁️ 7. Show the graph.
plt.show ()
• This last command will open a graph display window for the user.
📌 Summary
This code allows you to:
1.Load data from CSV file easily
2. Simple to understand each row data check
3. Create a bar graph to see each person's age trend.
You can extend this code immediately or ask me more. 😊
I have a little problem with PC. (Persona 😏) Now will come back down more often.
By the round ⚽️



![ภาพหน้าจอผลลัพธ์จากเทอร์มินัล แสดงข้อมูลที่อ่านจากไฟล์ CSV เป็นตาราง และการวนลูปแสดงข้อมูลแต่ละแถวในรูปแบบ 'Name: [ชื่อ], Age: [อายุ], Gender: [เพศ]'](https://p16-lemon8-sign-sg.tiktokcdn.com/tos-alisg-v-a3e477-sg/okiIea5eGGzZRQKzqMLCAfQI2TaqAoVe2AJhA5~tplv-sdweummd6v-wap-logo-v1:QHNwcml6ZXdvcms=:1080:0.webp?lk3s=66c60501&source=wap_large_logo_image&x-expires=1790488800&x-signature=Pbl5kdkoQ0fnx4fP%2BFtOVi9pEE4%3D)
















































































































