我要串联三列的值,这些值都被格式化为str以便能够获取完整的地址。第一个是单位编号,某些属性没有单位编号,因此我不想将那些缺少的单位编号值串联起来。如何添加if语句?
df['address_original'] = df['unit_number'].str.cat(df[['street_number', 'street_name']],
sep=', ')
print df['address_original'][:5]
0 , 184, VEALE ROAD
1 , 124, VEALE ROAD
2 , 1068, CLEARWATER VALLEY ROAD
3 , 1605, PINE STREET
4 , 1425, LOPEZ CREEK DRIVE
Name: address_original, dtype: object
这是原始数据框:
import pandas as pd
df = pd.DataFrame(data=search_results1)
print df.info()
print type(df)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2619 entries, 0 to 2618
Columns: 111 entries, access to zoning
dtypes: object(111)
memory usage: 2.2+ MB
None
这是address_original列中的当前内容
print df['address_original'][:3]
0 , 184, VEALE ROAD
1 , 124, VEALE ROAD
2 , 1068, CLEARWATER VALLEY ROAD
Name: address_original, dtype: object
答案 0 :(得分:1)
是吗?
df['address_original'] = np.where(df['unit_number'].isnull(), df['street_number'].astype(str)+','+df['street_name'].astype(str), df['unit_Number'].astype(str)+','+df['street_number'].astype(str)+','+df['street_name'].astype(str))
我仍然不确定目标列的外观
答案 1 :(得分:0)
尝试一下,可能对您有帮助
import numpy as np
import pandas as pd
data = pd.read_csv('c.csv')
print(data)
def GetFullAddress(unitno, add1, add2):
print(unitno, add1, add2)
sunitno = ''
if(unitno>0):
sunitno = str(unitno)+', '
return sunitno + add1 + ', '+add2
data['address'] = data.apply(lambda row: GetFullAddress(row['unitno'],row['add1'],row['add2']))
print(data)
使用的数据是:
unitno add1 add2
0 0 'address11' 'address21'
1 12 'address12' 'address22'
2 0 'address13' 'address23'
3 14 'address14' 'address24'
4 0 'address15' 'address25'