-1
import os 
cwd = os.getcwd()
df.to_csv(cwd+ "/BA_reviews.csv")

I didn't understand this code properly. Can someone help?

3 Answers3

1

os.getcwd docs

Return a string representing the current working directory.

so

import os 
cwd = os.getcwd()
df.to_csv(cwd+ "/BA_reviews.csv")

means read BA_reviews.csv from current working directory.

Daweo
  • 31,313
  • 3
  • 12
  • 25
0
import os

# os.getcwd() is a method in os module which gives the current directory
cwd = os.getcwd

# df is dataframe and to_csv is saving the df data in csv file 
#here file name is BA_reviews.csv
# you are basically concaneting the path and the filename 

df.to_csv(cwd+"/BA_reviews.csv")
0

Tha's just getting the absolute path of the current folder in order to save, from the pandas dataframe, a cvs file into that folder. You can either do the same using the built-in library specific for path operations, pathlib, and f-strings, in a more pythonic way:

import pathlib

cwd = pathlib.Path().absolute()
df.to_csv(f"{cwd}/BA_reviews.csv")
JonDel
  • 36
  • 4