I currently have a dataframe and a csv file.
In the csv file, I have about 30 columns, including one column of ZIP codes (eg, "10001", "08983", "85321"). CSV is 3400 rows x 30 columns.
In the dataframe, I have a column of ZIP codes and a matching column of scores:
zipcode score
99780 2.250000
99801 2.719083
99824 2.721311
99827 2.285714
99835 2.534783
99901 2.501558
[27688 rows x 1 columns]
I want to go to my csv file, look at each ZIP code, match the ZIP code up with the dataframe, and add a new column that notes the score that's stored in the dataframe. I would also be OK with just flat out replacing the ZIP code in the csv file with the score in the dataframe, if that's faster.
The reason why .replace
isn't going to work is because I don't want to manually enter all 27688 ZIP codes and say what those should be replaced by.
Any ideas?
EDIT : Here's what a row in the CSV file currently looks like:
Name Age DOB Gender Hair Color Eye Color ZIP
John Doe 22 6-10 Male Brn Brn 99780
And here's what I want it to look like:
Name Age DOB Gender Hair Color Eye Color ZIP Score
John Doe 22 6-10 Male Brn Brn 99780 2.250000
I'm also OK with the "Score" column flat-out replacing the ZIP column.
Have you tried loading the csv into a list and then looping through the items in the list.
import csv
sFile = "myfile.csv"
with open(sFile, 'rb') as f:
reader = csv.reader(f)
yourList = list(reader)
for item in yourList:
#search and replace based on each item
You can also try merge from pandas.
Update: Using Merge
Here is an example of using merge:
import pandas as pd
df = pd.DataFrame([[12345,"Atlanta"],[54321,"Orlando"]], columns=['Zip','City'])
df2 = pd.read_csv('Zip.csv')
df3 = pd.merge(df,df2,how="left", )
print (df3)
The result:
Zip City State
0 12345 Atlanda Georgia
1 54321 Orlando Florida
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.