我是python和pandas的新手,并且有一个读入熊猫数据框的csv文件。在下面找到它。
我正在尝试根据PLDATE中的行值填充列OND_ORIGIN和OND_DEST。
逻辑适用于同一天飞行的每个航班,OND_ORIGIN和OND_DEST应与departure_from和Arr_to列相同
import pandas as pd
import numpy as np
import csv
location = r'C:\Users\bi.reports\Desktop\output.csv'
df = pd.read_csv(location,sep='\s*,\s*',engine='python')
for i, row in df.iterrows():
if row['COUPON_NUMBER'] == 1:
df.OND_ORIGIN = df.DEP_FROM
#df.OND_DEST = df.DEP_FROM
elif row['COUPON_NUMBER'] == 2:
#df.OND_ORIGIN = df.DEP_FROM
df.OND_DEST = df.ARR_TO
elif row['COUPON_NUMBER'] == 3:
#df.OND_ORIGIN = df.DEP_FROM
df.OND_DEST = df.ARR_TO
else:
df.OND_ORIGIN = df.DEP_FROM
#df.OND_DEST = df.ARR_TO
df.to_csv('out.csv', sep=',',index = False)
答案 0 :(得分:0)
试试这个:
df.loc[df['COUPON_NUMBER'] == 1, 'OND_ORIGIN'] = df.DEP_FROM
df.loc[df['COUPON_NUMBER'].isin([2,3]), 'OND_DEST'] = df.ARR_TO
df.loc[~df['COUPON_NUMBER'].isin([1,2,3]), 'OND_ORIGIN'] = df.DEP_FROM
或位优化:
df.loc[df['COUPON_NUMBER'].isin([2,3]), 'OND_DEST'] = df.ARR_TO
df.loc[~df['COUPON_NUMBER'].isin([2,3]), 'OND_ORIGIN'] = df.DEP_FROM