Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas dataframe to key value pair

Tags:

pandas

What is the best way to convert following pandas dataframe to a key value pair

Before :

datetime             name    qty     price
2017-11-01 10:20     apple    5       1
2017-11-01 11:20     pear     2       1.5
2017-11-01 13:20     banana   10      5

After :

2017-11-01 10:20 name=apple qty=5 price=1
2017-11-01 11:20 name=pear  qty=2 price=1.5
2017-11-01 13:20 name=banana qty=10 price=5

note that i don't want the datetime key in my output.

like image 338
Sun Avatar asked Dec 04 '22 22:12

Sun


2 Answers

It seems you need to_dict:

d = df.drop('datetime', axis=1).to_dict(orient='records')
print (d)
[{'qty': 5, 'price': 1.0, 'name': 'apple'}, 
 {'qty': 2, 'price': 1.5, 'name': 'pear'}, 
 {'qty': 10, 'price': 5.0, 'name': 'banana'}]

but if need not key datetime:

d = df.set_index('datetime').to_dict(orient='index')
print (d)
{'2017-11-01 13:20': {'qty': 10, 'price': 5.0, 'name': 'banana'}, 
 '2017-11-01 10:20': {'qty': 5, 'price': 1.0, 'name': 'apple'}, 
 '2017-11-01 11:20': {'qty': 2, 'price': 1.5, 'name': 'pear'}}

If order is important:

tuples = [tup for tup in df.set_index('datetime').itertuples()]
print (tuples)

[Pandas(Index='2017-11-01 10:20', name='apple', qty=5, price=1.0), 
 Pandas(Index='2017-11-01 11:20', name='pear', qty=2, price=1.5), 
 Pandas(Index='2017-11-01 13:20', name='banana', qty=10, price=5.0)]

EDIT:

New DataFrame was created with column names and old values was added. Last write to_csv:

df = df.set_index('datetime').astype(str)
df1 = pd.DataFrame(np.tile(np.array(df.columns), len(df.index)).reshape(len(df.index), -1), 
                   index=df.index, 
                   columns=df.columns) + '='
df1 = df1.add(df)
print (df1)
                         name     qty      price
datetime                                        
2017-11-01 10:20   name=apple   qty=5  price=1.0
2017-11-01 11:20    name=pear   qty=2  price=1.5
2017-11-01 13:20  name=banana  qty=10  price=5.0

df1.to_csv('filename.csv', header=None)

2017-11-01 10:20,name=apple,qty=5,price=1.0
2017-11-01 11:20,name=pear,qty=2,price=1.5
2017-11-01 13:20,name=banana,qty=10,price=5.0
like image 95
jezrael Avatar answered Feb 07 '23 11:02

jezrael


If you are happy with a dictionary as output, you can use

df.to_dict('index')

On your example (with a slight parsing error for the dates by read_clipboard) this results in:

In [17]: df = pd.read_clipboard().reset_index(drop=True)

In [18]: df.to_dict('index')
Out[18]: 
{0: {'datetime': '10:20', 'name': 'apple', 'price': 1.0, 'qty': 5},
 1: {'datetime': '11:20', 'name': 'pear', 'price': 1.5, 'qty': 2},
 2: {'datetime': '13:20', 'name': 'banana', 'price': 5.0, 'qty': 10}}
like image 36
languitar Avatar answered Feb 07 '23 10:02

languitar