Python Forum
change item in column based on the item before[solved]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change item in column based on the item before[solved]
#1
Hello I would like to be able to compare a number with another number, and if the numbers are equal to each other, then I would like to add a number to the last number. Example:

[Var]
1
1
3
4
5
I would first compare lines 1 and 2 and as they are equal to each other then I would add a number to line 2 fx 1. Then I would like to compare lines 2 and 3, and as they are going to be different(line 2 = 2 now) then the variable should not be changed

I have tried doing it like this:
df = pd.read_csv("newfile1.csv", dtype=str)
n = 2
with open('newfile1.csv', 'r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    with open('newfile2.csv', 'w') as new_file:
        fieldnames = ['datetime', 'intensity']
        csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter=',')
        csv_writer.writeheader()
        for line in csv_reader:
            for i in range(len('datetime') - 1):
                if 'datetime'[i + 1] == 'datetime'[i]:
                    'datetime'[i + 1] = 'datetime'[i] + pd.DateOffset(minutes=n)
            csv_writer.writerow(line)
The datetime variable that i would like to change does however not change
Looking forward to hearing from you
Kind regards
Amdi

Attached Files

.xlsx   newfile1.xlsx (Size: 8.72 KB / Downloads: 200)
Reply


Messages In This Thread
change item in column based on the item before[solved] - by amdi40 - Jan-10-2022, 11:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  removing item pastakipp 3 1,974 Oct-25-2020, 09:15 PM
Last Post: deanhystad
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,137 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  need help removing an item from a list jhenry 4 4,280 Oct-13-2017, 08:15 AM
Last Post: buran
  Determine if a list contains a specific number of an item flannel_man 3 5,010 Nov-12-2016, 04:46 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020