Pandas系列名称未显示为数据帧的一部分

2024-09-25 18:26:17 发布

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

我正在将一个序列连接到一个数据帧上,但是列名(Series name)没有显示在新的数据帧上。在

相反,该列在最终数据帧中的名称为“0”,但在apply\u 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)

结果:

^{pr2}$

Tags: csv数据namedfreturndefelementssep
2条回答

您也可以尝试使用

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

如果您试图连接未知数量的列,可以将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,这样会更快。在

^{pr2}$

相关问题 更多 >