我从csv文件导入数据并将其存储在pandas数据帧中。这是来自csv文件的图像:
对于每一行,我想要一个这样的字符串:
以下是我用来从csv文件导入数据并将其存储在数据帧中的代码:
import csv
import pandas as pd
filename ="../Desktop/venkat.csv"
df = pd.read_table(filename,sep=" ")
我怎样才能做到这一点?
答案 0 :(得分:3)
我认为使用dict
来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
如果订购很重要,请使用OrderedDict
:
from collections import OrderedDict
print (OrderedDict(df.loc[2]))
OrderedDict([('A', 3), ('B', 6), ('C', 9), ('D', 5), ('E', 6), ('F', 3)])
如果您需要列中的所有值,请使用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
答案 1 :(得分:3)
考虑数据框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
您可以为每一行获得一系列json
个字符串
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}
答案 2 :(得分:0)
假设您的csv是逗号分隔文件。
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
答案 3 :(得分: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