Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

python - Update dataframe contents using itertuples

I have the following dataframe

| |st_id|Dep  |Arr   | 
|-|-----|-----|------|
|0|10021|00:15|      |
|1|10022|00:20|00:19 |
|2|10023|00:25|00:24 |
|3|10024|     |00:30 | 

I need to fill in empty Dep, Arr by condition if Arr =' ' then Arr = Dep if Dep=' 'then Dep=Arr. I can do it by iterrows like:

for index, row in df.iterrows():
    if df.at[index, 'Arr'] == '':
        df.at[index, 'Arr'] = df.at[index, 'Dep']
    if df.at[index, 'Dep'] == '':
        df.at[index, 'Dep'] = df.at[index, 'Arr']

but how to do it with itertuples I try

for row in df.itertuples():
    if row[3] == '':
        row[3] = row[2]
    if row[2] == '':
        row[2] = row[3]

then i have TypeError: 'Pandas' object does not support item assignment. What is the correct syntax of the assignment?

question from:https://stackoverflow.com/questions/65898945/update-dataframe-contents-using-itertuples

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...