1

I am importing data from csv file and storing it in pandas dataframe. Here is the image from the csv file:

example csv file

For each row I want a string like this:

output variables

Here is the code I am using to import data from the csv file and storing it in the dataframe:

import csv
import pandas as pd

filename ="../Desktop/venkat.csv"
df = pd.read_table(filename,sep=" ")

How can I achieve this?

4 Answers 4

3

Consider the dataframe df

df = pd.DataFrame(np.arange(10).reshape(-1, 5), columns=list('ABCDE'))

   A  B  C  D  E
0  0  1  2  3  4
1  5  6  7  8  9

You can get a series of json strings for each row

df.apply(pd.Series.to_json, 1)

0    {"A":0,"B":1,"C":2,"D":3,"E":4}
1    {"A":5,"B":6,"C":7,"D":8,"E":9}
Sign up to request clarification or add additional context in comments.

Comments

3

I think it's better to use a dict to save your data with to_dict:

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3

#select some row - e.g. with index 2
print (df.loc[2])
A    3
B    6
C    9
D    5
E    6
F    3
Name: 2, dtype: int64

d = df.loc[2].to_dict()
print (d)
{'E': 6, 'B': 6, 'F': 3, 'A': 3, 'C': 9, 'D': 5}

print (d['A'])
3

If ordering is important use OrderedDict:

from collections import OrderedDict

print (OrderedDict(df.loc[2]))
OrderedDict([('A', 3), ('B', 6), ('C', 9), ('D', 5), ('E', 6), ('F', 3)])

If you need all values in columns use DataFrame.to_dict:

d = df.to_dict(orient='list')
print (d)
{'E': [5, 3, 6], 'B': [4, 5, 6], 'F': [7, 4, 3], 
 'A': [1, 2, 3], 'C': [7, 8, 9], 'D': [1, 3, 5]}

print (d['A'])
[1, 2, 3]

d = df.to_dict(orient='index')
print (d)
{0: {'E': 5, 'B': 4, 'F': 7, 'A': 1, 'C': 7, 'D': 1}, 
1: {'E': 3, 'B': 5, 'F': 4, 'A': 2, 'C': 8, 'D': 3}, 
2: {'E': 6, 'B': 6, 'F': 3, 'A': 3, 'C': 9, 'D': 5}}


#get value in row 2 and column A 
print (d[2]['A'])
3

Comments

0
import csv
csvfile = open('test.csv','r')
csvFileArray = []
for row in csv.reader(csvfile, delimiter = '\t'):
    csvFileArray.append(row)

header =csvFileArray[0][0].split(',')

str_list=[]
for each in csvFileArray[1:]:
    data= each[0].split(',')
    local_list = []
    for i in range(len(data)):
        str_data =' '.join([header[i], '=', data[i]])
        local_list.append(str_data)
    str_list.append(local_list)

print str_list

Comments

0

Assume your csv is a comma delimited file.

import pandas as pd

filename ="../Desktop/venkat.csv"
df = pd.read_csv(filename,sep=",")
output = df.to_dict(orient='records')

print output #this would yield a list of dictionaries 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.