【问题标题】:Pandas Series name not showing up as part of a dataframe熊猫系列名称未作为数据框的一部分显示
【发布时间】:2017-10-02 18:45:27
【问题描述】:

我将一个系列连接到一个数据框中,但列名称(系列名称)未显示在新数据框上。

相反,该列在最终数据帧中的名称为“0”,但在 apply_join 方法中创建时,该名称确实出现了。

为什么在数据框中看不到系列名称?

import pandas as pd
from io import StringIO

tibble3_csv = """country,year,cases,population
Afghanistan,1999,745,19987071
Afghanistan,2000,2666,20595360"""
with StringIO(tibble3_csv) as fp:
    tibble3 = pd.read_csv(fp)

 def str_join_elements(x, sep=""):
    assert type(sep) is str
    return sep.join((str(xi) for xi in x))

 def unite(df, cols, new_var, combine=str_join_elements):
    def apply_join(x, combine):
         joinstr = combine(x)
         ser = pd.Series(joinstr, name=new_var)
         print(ser.name)
        return ser

     fixed_vars = df.columns.difference(cols)
     tibble = df[fixed_vars].copy()
     tibble_extra = df[cols].apply(apply_join, combine=combine, axis=1)

     return pd.concat([tibble, tibble_extra], axis=1) 

 tab = unite(tibble3, ['cases', 'population'], 'rate', combine=lambda x: str_join_elements(x, "/"))
 print(tab)

结果:

rate
rate
       country  year                  0
 0  Afghanistan  1999       745/19987071
1  Afghanistan  2000      2666/20595360

【问题讨论】:

    标签: python pandas dataframe series


    【解决方案1】:

    如果您尝试连接未知数量的列,您可以使用applystr.join

    def foo(df, columns, col_name, sep=''):
        s = df[columns].apply(lambda x: sep.join(map(str, x)), 1)
        s.name = col_name
        return pd.concat([df[df.columns.difference(columns)], s], axis=1)
    
    df
           country  year  cases  population
    0  Afghanistan  1999    745    19987071
    1  Afghanistan  2000   2666    20595360
    
    df2 = foo(df, ['cases', 'population'], 'rate', '/')
    df2
           country  year           rate
    0  Afghanistan  1999   745/19987071
    1  Afghanistan  2000  2666/20595360
    

    如果总是两列,你可以使用str.cat,这样会快很多。

    def foo2(df, c1, c2, c3, sep=''):
        s1, s2 = df[c1].astype(str), df[c2].astype(str)
        s3 = s1.str.cat(s2, sep=sep)
        s3.name = c3
        return pd.concat([df[df.columns.difference([c1, c2])], s3], axis=1)
    
    df2 = foo2(df, 'cases', 'population', 'rate', '/')
    df2
           country  year           rate
    0  Afghanistan  1999   745/19987071
    1  Afghanistan  2000  2666/20595360
    

    【讨论】:

    • 我喜欢你的简单解决方案。然而,这是一个赋值,“foo/unite”的方法签名包括一个正在传递的函数,在本例中为“combine”。
    • @cumin 好吧,在调用pd.concat 之前使用tibble_extra.name = 'rate'。 ://
    • @cumin 原始数据框没有命名列的原因是tibble_extra 的结果没有名称。您需要为其分配一个。此名称稍后将成为输出 df 的一部分。
    • 我必须在 `tibble_extra.columns =[new_var]` 中添加它似乎很笨拙,但看起来我这样做了。那行得通
    【解决方案2】:

    您也可以尝试使用重命名列

    >>> tab = tab.rename(columns = {0:'cases/population'})
    >>> tab
           country  year cases/population
    0  Afghanistan  1999     745/19987071
    1  Afghanistan  2000    2666/20595360
    >>> 
    

    【讨论】:

    • 谢谢,这会奏效。但是,当在print(ser.name) 制作系列时,为什么列的名称(“rate”)没有出现在 df 中?
    猜你喜欢
    • 2018-08-16
    • 1970-01-01
    • 2018-02-17
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2018-08-17
    • 2019-10-12
    相关资源
    最近更新 更多