如何将一个单元格值从一个工作表(当前为浮点数)复制到另一个具有空白单元格(NaN)的工作表?以下是我尝试过的步骤。
例如; 我有这个值(510.0):
>>> parcel_csv[parcel_csv.PID == '570-177736'].LANDUSE
341817 510.0
Name: LANDUSE, dtype: float64
我希望在这里分配它:
>>> df.loc[df['Parcel ID']=='570-177736',['Land Use']]
Land Use
0 NaN
基于其他问题,这应该可行:
>>> df.loc[df['Parcel ID']=='570-177736',['Land Use']] = parcel_csv[parcel_csv.PID == '570-177736'].LANDUSE
我似乎仍然以NaN结尾。因此,我尝试首先分配一个值:
>>> df.loc[df['Parcel ID']=='570-177736',['Land Use']] = '111.0'
>>> df.loc[df['Parcel ID']=='570-177736',['Land Use']]
Land Use
0 111.0
再次尝试“复制”:
>>> df.loc[df['Parcel ID']=='570-177736',['Land Use']] = parcel_csv[parcel_csv.PID == '570-177736'].LANDUSE
并再次以NaN结尾:
>>> df.loc[df['Parcel ID']=='570-177736',['Land Use']]
Land Use
0 NaN
在源单元格上使用.loc会得到相同的结果:
>>> parcel_csv.loc[parcel_csv.PID == '570-177736'].LANDUSE
341817 510.0
Name: LANDUSE, dtype: float64
>>> df.loc[df['Parcel ID']=='570-177736',['Land Use']] = parcel_csv.loc[parcel_csv.PID == '570-177736'].LANDUSE
>>> df.loc[df['Parcel ID']=='570-177736',['Land Use']]
Land Use
0 NaN
Python 3.6.7
谢谢!