-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_excercise.py
39 lines (30 loc) · 1.41 KB
/
csv_excercise.py
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
import pandas as pd
def add_full_name(path_to_csv, path_to_new_csv):
#Assume you will be reading in a csv file with the same columns that the
#Lahman baseball data set has -- most importantly, there are columns
#called 'nameFirst' and 'nameLast'.
#1) Write a function that reads a csv
#located at "path_to_csv" into a pandas dataframe and adds a new column
#called 'nameFull' with a player's full name.
#
#For example:
# for Hank Aaron, nameFull would be 'Hank Aaron',
#
#2) Write the data in the pandas dataFrame to a new csv file located at
#path_to_new_csv
#WRITE YOUR CODE HERE
dataframe=pd.read_csv(path_to_csv)
dataframe['nameFull']=dataframe['nameFirst']+' '+dataframe['nameLast']
dataframe.to_csv(path_to_new_csv)
# print(path_to_csv.namefirst)
# grouped = path_to_new_csv.groupby(['path_to_csv.namefirst', 'path_to_csv.lastfirst'], as_index=False).sum()
return 0
if __name__ == "__main__":
# For local use only
# If you are running this on your own machine add the path to the
# Lahman baseball csv and a path for the new csv.
# The dataset can be downloaded from this website: http://www.seanlahman.com/baseball-archive/statistics
# We are using the file Master.csv
path_to_csv = 'baseballdatabank-master/core/Master.csv'
path_to_new_csv='baseballdatabank-master/core/Master.csv'
add_full_name(path_to_csv, path_to_new_csv)