简体   繁体   English

Pandas 使用来自其他 2 列的字符串值填充

[英]Pandas fillna with string values from 2 other columns

I have a df with 3 columns, City, State, and MSA.我有一个包含 3 列的 df,City、State 和 MSA。 Some of the MSA values are NaN.一些 MSA 值是 NaN。 I would like to fill the MSA NaN values with a concatenation of City and State.我想用 City 和 State 的串联填充 MSA NaN 值。 I can fill MSA with City using df.MSA_CBSA.fillna(df.City, inplace=True) , but some cities in different states have the same name.我可以使用df.MSA_CBSA.fillna(df.City, inplace=True)用 City 填充 MSA,但是不同州的一些城市具有相同的名称。

City城市 State State MSA MSA
Chicago芝加哥 IL伊利诺伊州 Chicago MSA芝加哥海事局
Belleville贝尔维尔 IL伊利诺伊州 Nan
Belleville贝尔维尔 KS KS Nan
City城市 State State MSA MSA
Chicago芝加哥 IL伊利诺伊州 Chicago MSA芝加哥海事局
Belleville贝尔维尔 IL伊利诺伊州 Belleville IL伊利诺伊州贝尔维尔
Belleville贝尔维尔 KS KS Belleville KS堪萨斯州贝尔维尔

Keep using the vectorized operation that you suggested.继续使用您建议的矢量化操作。 Notice that the argument can receive a combination from the other instances:请注意,参数可以接收来自其他实例的组合:

df.MSA.fillna(df.City + "," + df.State, inplace=True) df.MSA.fillna(df.City + "," + df.State, inplace=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM