【问题标题】:Convert pandas Series to DataFrame将熊猫系列转换为 DataFrame
【发布时间】:2014-11-23 17:29:39
【问题描述】:

我有一个熊猫系列科幻小说:

email
email1@email.com    [1.0, 0.0, 0.0]
email2@email.com    [2.0, 0.0, 0.0]
email3@email.com    [1.0, 0.0, 0.0]
email4@email.com    [4.0, 0.0, 0.0]
email5@email.com    [1.0, 0.0, 3.0]
email6@email.com    [1.0, 5.0, 0.0]

我想将其转换为以下 DataFrame:

index | email             | list
_____________________________________________
0     | email1@email.com  | [1.0, 0.0, 0.0]
1     | email2@email.com  | [2.0, 0.0, 0.0]
2     | email3@email.com  | [1.0, 0.0, 0.0]
3     | email4@email.com  | [4.0, 0.0, 0.0]
4     | email5@email.com  | [1.0, 0.0, 3.0]
5     | email6@email.com  | [1.0, 5.0, 0.0]

我找到了一种方法,但我怀疑它是否更有效:

df1 = pd.DataFrame(data=sf.index, columns=['email'])
df2 = pd.DataFrame(data=sf.values, columns=['list'])
df = pd.merge(df1, df2, left_index=True, right_index=True)

【问题讨论】:

标签: python pandas dataframe series


【解决方案1】:

您可以使用 DataFrame 构造函数在 dict 中将它们作为参数传递,而不是创建 2 个临时 dfs:

pd.DataFrame({'email':sf.index, 'list':sf.values})

构造df的方法有很多,见docs

【讨论】:

  • 如果您的系列具有相同的轴,则另一个不错的选择是 concat pd.concat([sf.index, sf.values], axis=1)
【解决方案2】:

to_frame()

从以下系列开始,df:

email
email1@email.com    A
email2@email.com    B
email3@email.com    C
dtype: int64

我使用 to_frame 将系列转换为 DataFrame:

df = df.to_frame().reset_index()

    email               0
0   email1@email.com    A
1   email2@email.com    B
2   email3@email.com    C
3   email4@email.com    D

现在你只需要重命名列名并命名索引列:

df = df.rename(columns= {0: 'list'})
df.index.name = 'index'

您的 DataFrame 已准备好进行进一步分析。

更新:我刚刚遇到this link,这里的答案与我的惊人相似。

【讨论】:

  • series_obj.to_frame() 有效!我输出这个类类型<class 'pandas.core.frame.DataFrame'>
  • 为什么使用to_frame().reset_index() 而不仅仅是reset_index?你甚至可以只做reset_index(name='list')
【解决方案3】:

单行答案是

myseries.to_frame(name='my_column_name')

或者

myseries.reset_index(drop=True, inplace=True)  # As needed

【讨论】:

  • 我需要在 seaborn 条形图中使用转换后的 DataFrame obj,所以我需要重置索引,比如 mydf = myseries.to_frame(name='my_column_name').reset_index()
【解决方案4】:

Series.reset_index 带有 name 参数

通常会出现需要将 Series 提升为 DataFrame 的用例。但是如果系列没有名字,那么reset_index 会产生类似的结果,

s = pd.Series([1, 2, 3], index=['a', 'b', 'c']).rename_axis('A')
s

A
a    1
b    2
c    3
dtype: int64

s.reset_index()

   A  0
0  a  1
1  b  2
2  c  3

您看到的列名称是“0”。我们可以通过指定 name 参数来解决此问题。

s.reset_index(name='B')

   A  B
0  a  1
1  b  2
2  c  3

s.reset_index(name='list')

   A  list
0  a     1
1  b     2
2  c     3

Series.to_frame

如果您想在不将索引提升到列的情况下创建 DataFrame,请使用Series.to_frame,如this answer 中所建议的那样。这支持名称参数。

s.to_frame(name='B')

   B
A   
a  1
b  2
c  3

pd.DataFrame构造函数

您也可以通过指定columns 参数来执行与Series.to_frame 相同的操作:

pd.DataFrame(s, columns=['B'])

   B
A   
a  1
b  2
c  3

【讨论】:

  • 我想知道为什么有人可以使用to_frame 而不是reset_index,但有没有充分的理由同时使用两者? here
  • @dumbledad 主要是实用程序。如果您想要一个带有索引的 col 数据帧,请使用 to_frame()。如果您需要两列(一列来自系列索引,另一列来自系列值本身),请使用 reset_index()。
  • 如果我想将 Series 转换为 DataFrame 并将 Seires 索引用作 DataFrame 列名(即转置)怎么办? to_frame 似乎没有理由这样做。谢谢。
  • @Confounded 使用 to_frame().T 转置它
【解决方案5】:

方法也超级简单

df = pd.DataFrame(series)

它将返回 1 列(系列值)+ 1 个索引(0....n)的 DF

【讨论】:

    【解决方案6】:

    Series.to_frame 可用于将Series 转换为DataFrame

    # The provided name (columnName) will substitute the series name
    df = series.to_frame('columnName')
    

    例如,

    s = pd.Series(["a", "b", "c"], name="vals")
    df = s.to_frame('newCol')
    print(df)
    
       newCol
    0    a
    1    b
    2    c
    

    【讨论】:

      【解决方案7】:

      可能被评为非pythonic方式来做到这一点,但这会在一行中给出你想要的结果:

      new_df = pd.DataFrame(zip(email,list))
      

      结果:

                     email               list
      0   email1@email.com    [1.0, 0.0, 0.0]
      1   email2@email.com    [2.0, 0.0, 0.0]
      2   email3@email.com    [1.0, 0.0, 0.0]
      3   email4@email.com    [4.0, 0.0, 3.0]
      4   email5@email.com    [1.0, 5.0, 0.0]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-12
        • 1970-01-01
        相关资源
        最近更新 更多