单独的文本和数字,并在python中转换为dataframe

时间:2015-02-17 13:11:03

标签: python list pandas

我有一个这样的清单:

[['fast_ema_period=22', ' slow_ema_period=10', ' signal_period=34', ' StopLoss=190', ' TakeProfit=140', ' Slippage=1', ' MagicNumber=0', ' '],

['fast_ema_period=4', ' slow_ema_period=22', ' signal_period=14', ' StopLoss=180', ' TakeProfit=170', ' Slippage=1', ' MagicNumber=0', ' '],

['fast_ema_period=32', ' slow_ema_period=10', ' signal_period=24', ' StopLoss=180', ' TakeProfit=120', ' Slippage=1', ' MagicNumber=0', ' '],

['fast_ema_period=40', ' slow_ema_period=46', ' signal_period=36', ' StopLoss=40', ' TakeProfit=130', ' Slippage=1', ' MagicNumber=0', ' ']]

我想将其转换为大熊猫' dataframe with:

列:[fast_ema_period, slow_ema_period, signal_period, StopLoss,TakeProfit, Slippage, MagicNumber]

和表格正文中的值。

提前非常感谢你。

1 个答案:

答案 0 :(得分:0)

简单。

from collections import defaultdict
# Use a default dict to make our life easier (we already know we are working with lists)
data = defaultdict(list)
# Get each item from each list, the below list-comprehension is efficient.
for item in [item for sublist in biglist for item in sublist if '=' in item]:
    key, value = item.split('=', 1)  # split the items on '=' but only once.
    data[key].append(value)  # add our new value to our dictionary. 

pd.DataFrame(data)  # tada!