-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmycode.py
More file actions
44 lines (35 loc) · 1005 Bytes
/
mycode.py
File metadata and controls
44 lines (35 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import pandas as pd
import os
# create a sample dataframe with column names
data = {
'Name' : ['Alice','Bob','Charlie'],
'Age' : [25,30,35],
'City' : ['New York','Los Angeles','Chicago']
}
df = pd.DataFrame(data)
## Adding new row to df for v2
new_row_loc = {
'Name' : 'David',
'Age' : 28,
'City' : 'San Francisco'
}
df = pd.concat([df,pd.DataFrame([new_row_loc])],ignore_index=True)
# adding new row to df for v3
new_row_loc_v3 = {
'Name' : 'Eva',
'Age' : 22,
'City' : 'Boston'
}
df = pd.concat(
[df,pd.DataFrame([new_row_loc_v3])],
ignore_index = True
)
# ensure the "data" directory exists at the root level
data_dir = 'data'
os.makedirs(data_dir,exist_ok=True)
# define the file path
# joins from the root directory to the "data" folder and the desired file name
file_path = os.path.join(data_dir,'sample_data.csv')
# save the dataframe to a csv file, including column names
df.to_csv(file_path,index=False)
print(f"CSV file saved to {file_path}")