Python:将字符串[(' a',0.2),(' b',0.9),(' a',0.4)]转换为数据帧

时间:2016-11-06 03:44:45

标签: python string

在Python中,如何转换像

这样的字符串
thisStr = '[('a', 0.332), ('d', 0.43766), ('b', 0.3244), ('b', 0.76577), ('a', 0.863), ('d', 0.96789)]'

进入像

这样的DataFrame
index   item     value
0       a        0.332
1       d        0.43766
2       b        0.3244
3       b        0.76577
4       a        0.863
5       d        0.96789

2 个答案:

答案 0 :(得分:1)

听起来你正在寻找将字符串更改为pandas数据帧然后进行一些操作。我通过一些简单的替换和字符串开头和结尾的手动编辑将字符串更改为以下内容。除了结尾之外,您可以转义标点符号,以便您可以应用eval()函数。

import pandas as pd

thisStr = eval('[(\'a\', 0.332), (\'d\', 0.43766), (\'b\', 0.3244), (\'b\', 0.76577), (\'a\', 0.863), (\'d\', 0.96789)]')

df = pd.DataFrame(thisStr)
df.rename(columns={0:'item', 1:'value'}, inplace=True)

# one approach to solving the problem of removing rows where
# item a has values less than 0.8.
mask = (df['item'] == 'a') & (df['value'] < 0.8)
df2 = df[~mask]

答案 1 :(得分:1)

使用eval函数将字符串转换为元组列表

# change to double quote " because contains single quote within string
thisStr = "[('a', 0.332), ('d', 0.43766), ('b', 0.3244), ('b', 0.76577), ('a', 0.863), ('d', 0.96789)]"

# this turn the string into list of tuples
mylist = eval(thisStr)
# mylist[0][0] access 1st row item which is 'a'
# mylist[0][1] access 1st row value which is 0.332

# to remove all row 'a' less than 0.8
newlist = [i for i in mylist if not (i[0]=='a' and i[1] < 0.8)]