← Back to NumPy Course | Chapter 11: File I/O | Lesson 4 of 5

CSV के साथ काम करना

CSV files बस commas वाली text हैं, इसलिए loadtxt और genfromtxt इन्हें पढ़ सकते हैं और savetxt इन्हें लिख सकता है।
Syntax
python
data = np.loadtxt("file.csv", delimiter=",", skiprows=1)
np.savetxt("out.csv", data, delimiter=",")

CSV के साथ काम करना

पूरी तरह numeric CSV files के लिए, NumPy के text functions काफी हैं। Titles के लिए skiprows या skip_header और columns pick करने के लिए usecols इस्तेमाल करें। Mixed text और numbers के लिए, Pandas आमतौर पर ज़्यादा convenient है।

Note: genfromtxt के साथ dtype=None और names=True mixed columns से एक structured array बनाते हैं।

उदाहरण: Working with CSV

python
import numpy as np
import io

csv = "height,weight\n170,65\n180,80\n160,50"
data = np.loadtxt(io.StringIO(csv), delimiter=",", skiprows=1)
print(data)
print("mean weight:", data[:, 1].mean())

# Output:
# [[170.  65.]
#  [180.  80.]
#  [160.  50.]]
# mean weight: 65.0
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. text columns को एक float array में load करने की कोशिश करना
  2. header row skip करना भूल जाना
  3. जहाँ Pandas आसान होता वहाँ NumPy इस्तेमाल करना
चैप्टर सारांश
  • Header row skip करें
  • usecols columns select करता है
  • Numeric-only CSVs NumPy के लिए suit करती हैं
  • Mixed data के लिए Pandas इस्तेमाल करें
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.