join.str() 给出 Nan 值

时间:2021-03-01 11:00:33

标签: python pandas string dataframe

当我尝试将 str 与 ' ' sep 连接时,我得到了 Nan 值。下面是代码和输出。

df_groupby_company_10.ID 公司 (dah)bryan Isd [100645170, 102320579, 102565211, 102797936] 1-800-Flowers.com, Inc. [100054859, 100066805, 100224589, 100497986] 1199seiu 联合医护东 [100123809, 100392880, 100512699, 103499749] 一源公司 [100345458, 100349515, 100472860, 100519219] 2020 Company, LLC [100213385, 100245719, 100248099, 100333687] 21世纪保险公司[100036144、100258483、100264759、100405599]

df_groupby_company_10['ID'].str.join(' ') 公司 (dah)bryan Isd NaN 1-800-Flowers.com, Inc. NaN 1199seiu 联合卫生保健工作者东 NaN 1st Source Corporation NaN 2020 年公司有限责任公司 NaN 21世纪保险公司NaN

但它应该给出类似于 ID 列的结果: 103730851 122564134 137669806 138117993 115079368 117608628 118146856 123930476

谁能告诉我我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

列表中的值是整数,需要将它们转换为字符串:

df_groupby_company_10['ID'].apply(lambda x: [str(y) for y in x]).str.join(' ')

或者:

df_groupby_company_10['ID'].apply(lambda x: ' '.join(str(y) for y in x))