用 pandas 中的其他列填充 Nas
fill Nas with other column in pandas
我有一些 NA,想用同一行另一列中的值填充它们。
df = pd.DataFrame({"column A": ["Atlanta", "Atlanta", "New York", "",""],
"column B": ["AT", "AT", "NY", "1", ""]})
columnA
ColumnB
Atlanta
AT
Atlanta
AT
Newyork
NY
1
预期输出:
columnA
ColumnB
Atlanta
AT
Atlanta
AT
Newyork
NY
1
1
您可以像这样简单地将 fillna
与列名称一起使用:
df['column A'].fillna(df['column B'])
我有一些 NA,想用同一行另一列中的值填充它们。
df = pd.DataFrame({"column A": ["Atlanta", "Atlanta", "New York", "",""],
"column B": ["AT", "AT", "NY", "1", ""]})
columnA | ColumnB |
---|---|
Atlanta | AT |
Atlanta | AT |
Newyork | NY |
1 | |
预期输出:
columnA | ColumnB |
---|---|
Atlanta | AT |
Atlanta | AT |
Newyork | NY |
1 | 1 |
您可以像这样简单地将 fillna
与列名称一起使用:
df['column A'].fillna(df['column B'])