如何将单个列表转换为具有多列的数据帧?

2024-09-19 23:39:15 发布

您现在位置:Python中文网/ 问答频道 /正文

List = ['Stevie', '$101', '(33%)', 'Hezazeb', '$3.60', '(85%)', 
        'Boga Dreams', '$5.50', '(25%)', 'Grey Detective', '$8.00', '(22%)', 
        'Bring A Dame', '$26.00', '(8%)', 'Sandhill', '$5.00', '(100%)', 
        'Burgundy Rules', '$41.00', '(17%).', 'Luxitorah', '$7.50', '(0%)', 
        'Play On Words', '$21.00', '(14%).', 'Cranky Sheriff', '$13.00', '(8%)']

我希望此列表以以下方式存储在具有3列的数据框中。我正在从网站获取此数据,因此无法手动执行此操作

- Playername Bids probability   
- Stevie.    $101.   33% 
- Hezazeb.   $3.60.  85%
- .
- .
- .

等等


Tags: 数据playruleslistgreybringdetectivedreams
2条回答

要将列表转换为数据帧,您应该有tuples或pd.series或任何数据帧对象,在您的情况下,我建议手动将它们转换为dict,因为每个点的数据都非常不同,然后用nan填充缺少的列

这甚至适用于非固定数据-您只需预处理:

# jumbled data needs preprocessing - if you have cleaner data skip that step
data = ['Stevie', '$101', '(33%)', 'Hezazeb', '$3.60', '(85%)', 'Boga', 
'Dreams', '$5.50', '(25%)', 'Grey', 'Detective', '$8.00', '(22%)', 'Bring', 
'A', 'Dame', '$26.00', '(8%)', 'Sandhill', '$5.00', '(100%)', 'Burgundy', 
'Rules', '$41.00', '(17%).', 'Luxitorah', '$7.50', '(0%)', 'Play', 'On', 
'Words', '$21.00', '(14%).', 'Cranky', 'Sheriff', '$13.00', '(8%)']

# Preprocess: 
# join all into one string
# fix irritating ). to be )
# fix $ to be |$ 
# fix ) to be )|  to enable splitting at | for well formed data
d = " ".join(data).replace(").", ")").replace(")", 
    ")|").replace("$", "|$").replace("(", "|(")

# remove pending last |
# split at | into well formatted list
d = d.rstrip("|").split("|")

import pandas as pd

# use list slicing to fill dataframe from source list
df = pd.DataFrame({"Name": d[0::3], "Bet": d[1::3], "probability": d[2::3]})

print(df)

输出:

               Name      Bet probability
0           Stevie     $101        (33%)
1          Hezazeb    $3.60        (85%)
2      Boga Dreams    $5.50        (25%)
3   Grey Detective    $8.00        (22%)
4     Bring A Dame   $26.00         (8%)
5         Sandhill    $5.00       (100%)
6   Burgundy Rules   $41.00        (17%)
7        Luxitorah    $7.50         (0%)
8    Play On Words   $21.00        (14%)
9   Cranky Sheriff   $13.00         (8%)

如果您不清楚,请参阅Understanding slice notation

相关问题 更多 >