【问题标题】:How to convert list object to Pandas Dataframe with defined format?如何将列表对象转换为具有定义格式的 Pandas Dataframe?
【发布时间】:2016-09-24 23:38:42
【问题描述】:

如何将列表对象转换为具有定义格式的 Pandas Dataframe?

打印列表有这个输出(Jupyter):

(u'variable',    price  threshold
0   13.5      100.0
1   10.0        NaN)
(u'standard',    price  threshold
0   12.5      300.0
1   11.0        NaN)
(u'fixed',    price  threshold
0   14.5      250.0
1   10.1      200.0
2    9.0        NaN)
(u'standing-charge',    price
0      9)

转换为 Pandas 数据框:

    0               1
0   variable        price threshold 0 13.5 100.0 1 10...
1   standard        price threshold 0 12.5 300.0 1 11...
2   fixed           price threshold 0 14.5 250.0 1 10...
3   standing-charge price 0 9

我需要:

type       price  threshold
variable    13.5   100
variable    10.0   NaN
...

请帮忙;)

【问题讨论】:

  • 这些列表中的元素是什么?数据帧?
  • 哦,你DataFrame的索引必须是唯一的,所以你的I need是不可能的。
  • 解析json文件的输出:rows_list = [] for index, row in ds.iterrows(): a = {} a = row['plan'], json_normalize(row['rates' ]) rows_list.append(a) 打印 rows_list
  • type(rows_list) 是列表
  • 如何解析这种格式?还没见过类似的格式。

标签: python list pandas dataframe


【解决方案1】:

在您清除列表中的内容后,您可以执行以下操作:

df = pd.DataFrame()
for key, df2 in row_list:
    df2['index'] = key
    df = df.append(df2, ignore_index=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 2017-02-18
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    相关资源
    最近更新 更多