[英]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 ![]() |
MSA ![]() |
---|---|---|
Chicago![]() |
IL![]() |
Chicago MSA![]() |
Belleville![]() |
IL![]() |
Nan![]() |
Belleville![]() |
KS ![]() |
Nan![]() |
City![]() |
State ![]() |
MSA ![]() |
---|---|---|
Chicago![]() |
IL![]() |
Chicago MSA![]() |
Belleville![]() |
IL![]() |
Belleville IL![]() |
Belleville![]() |
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.