Reputation: 2590
I have a csv file which has contents like the following:
stores.csv
Site, Score, Rank
roolee.com,100,125225
piperandscoot.com,29.3,222166
calledtosurf.com,23.8,361542
cladandcloth.com,17.9,208670
neeseesdresses.com,9.6,251016
...
Here's my model.
models.py
class SimilarStore(models.Model):
store = models.ForeignKey(Store)
csv = FileField()
domain = models.CharField(max_length=100, blank=True)
score = models.IntegerField(blank=True)
rank = models.IntegerField(blank=True)
So, I wanna upload the stores.csv
file into csv
field so that each column data goes into each domain
, score
, and rank
.
I found out some resources making python file to parse data and run it through a command like `python parse.py'. However, I need it to be done by uploading the csv file in Django admin. Can anyone explain how I can do that?
Upvotes: 0
Views: 1337
Reputation: 652
Just find the table name in your Database also find the corresponding column names with the table and use the to_sql command in pandas
Upvotes: 0
Reputation: 472
You can read the csv file through pandas and then convert it to your desired object ny using pandas funtion 'infer_objects()'. I hope the code below helps you in this regard,
import pandas as pd
SimilarStore_df = pd.read_csv('./store.csv') #importing csv file as pandas DataFrame
SimilarStore_df.columns = ['Domain', 'Score','Rank']
SimilarStore=SimilarStore_df.infer_objects() #converting DataFrame to object
print(SimilarStore)
print(SimilarStore.columns)
Output:
Domain Score Rank
0 roolee.com 100.0 125225
1 piperandscoot.com 29.3 222166
2 calledtosurf.com 23.8 361542
3 cladandcloth.com 17.9 208670
4 neeseesdresses.com 9.6 251016
Index(['Domain', 'Score', 'Rank'], dtype='object')
Upvotes: 2