← Back to Pandas Course | Chapter 11: File I/O | Lesson 1 of 7

read_csv और to_csv

read_csv किसी CSV file को DataFrame में load करता है और to_csv उसे वापस लिख देता है।

In this page:

  1. read_csv/to_csv
Syntax
python
df = pd.read_csv("file.csv", sep=",", header=0)
df.to_csv("out.csv", index=False)

read_csv/to_csv

pd.read_csv(path) comma-separated text पढ़ता है, जिसमें sep, header, usecols, dtype, parse_dates और na_values जैसे options होते हैं। to_csv(path, index=False) row index के बिना एक file लिखता है। path एक URL या in-memory buffer भी हो सकता है।

Note: to_csv को index=False पास करें जब तक आपको index की ज़रूरत न हो।

उदाहरण: read_csv/to_csv

python
import io
import pandas as pd

csv = "name,age,joined\nAnn,28,2024-01-05\nBob,35,2024-02-10"
df = pd.read_csv(io.StringIO(csv), parse_dates=["joined"])
print(df)
print(df.dtypes)
print(df.to_csv(index=False))

# Output:
#   name  age     joined
# 0  Ann   28 2024-01-05
# 1  Bob   35 2024-02-10
# name              object
# age                int64
# joined    datetime64[ns]
# dtype: object
# name,age,joined
# Ann,28,2024-01-05
# Bob,35,2024-02-10
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. index को एक extra column के रूप में लिखना
  2. dates parse न करना
  3. सिर्फ कुछ columns चाहिए होने पर भी सभी columns load करना
चैप्टर सारांश
  • read_csv CSV load करता है
  • to_csv उसे लिखता है
  • usecols और dtype memory बचाते हैं
  • index=False index को skip करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.