The following demographic data.
I want to put the prefecture name where the city name is NaN.
Group code Prefecture name Municipality name Gender Total number 0-4 years old 5-9 10-14 15-19\
3 10006.0 Hokkaido*NaN total 5339539 181591 201119 213206 231870
4 10006.0 Hokkaido*NaN Man 2522526 93048 102850 108783 118414
5 10006.0 Hokkaido*NaN Woman 2817013 88543 98269 104423 113456
6 11002.0 Sapporo, Hokkaido Total 1952348 71281 75025 76342 82644
7 11002.0 Sapporo, Hokkaido Man 913077 36715 38250 38839 41729
df['City name'].fillna(df['Name of prefectures'], inplace=True)
#It's possible here, but it seems to be slower.
df.loc[pd.isnull(df['City name']), ['City name']] = df['Name of prefectures']
for i, (prefecture, city) in enumerate(zip(df['Name of prefectures'], df['City name'])):
if pd.isnull(city):
df['City name'][i] = prefecture
result
<ipython-input-59-8ff91f161741>:3: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df['City name'][i] = prefecture