pandas 熊猫数据帧到键值对

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43185659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:20:13  来源:igfitidea点击:

pandas dataframe to key value pair

pandas

提问by Sun

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

将以下Pandas数据帧转换为键值对的最佳方法是什么

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.

请注意,我不希望在我的输出中使用 datetime 键。

回答by jezrael

It seems you need to_dict:

看来你需要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:

但如果不需要关键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 DataFramewas created with column names and old values was added. Last write to_csv:

DataFrame使用列名创建了新值,并添加了旧值。最后写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

回答by languitar

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:

在您的示例中(日期 by 有轻微的解析错误read_clipboard)这导致:

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}}

回答by Charan P

If you exactly need the output you specified one possible solution is to iterate over the dataframe by rows and construct a string by zipping columns list with row values. A crude version is:

如果您确实需要您指定的输出,则一种可能的解决方案是按行迭代数据帧,并通过使用行值压缩列列表来构造一个字符串。一个粗略的版本是:

def create_key_vals(iterable):
    str_list = []
    for ite in iterable:
        str_list.append(ite[0]+"="+ str(ite[1]))
    return ','.join(str_list)

new_df = df.drop('datetime', axis=1)
col_list = new_df.columns.tolist()
zipped_col_vals = [zip(col_list, row) for row in new_df.itertuples(index=False, name=False)]
col_value_list = [create_key_vals(ite) for ite in zipped_col_vals]
In [116]: col_value_list
Out[116]:
['name=apple,price=1.0,qty=5',
 'name=pear,price=1.5,qty=2',
 'name=banana,price=5.0,qty=10']

You can convert this into a string by:

您可以通过以下方式将其转换为字符串:

In [117]: ('\n').join(col_value_list)
Out[117]: 'name=apple,price=1.0,qty=5\nname=pear,price=1.5,qty=2\nname=banana,price=5.0,qty=10