2017-02-10 48 views
1

我從csv文件導入數據並將其存儲在pandas數據框中。下面是CSV文件中的圖像:將列名轉換爲python中的變量

example csv file

對於每一行我想要一個像這樣的字符串:

output variables

這裏是我使用從CSV導入數據的代碼文件並將其存儲在數據框中:

import csv 
import pandas as pd 

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

我該如何實現這一目標?

回答

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 
3

我認爲這是更好地使用dictto_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 
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} 
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 
相關問題