0

I'm sort of new to using pandas in Python for data visualization, and have this code so far:

import pandas as pd
data = pd.read_csv(r'/Users/ephemeralhappiness/Desktop/Packet/Fake Data.csv')
df = pd.DataFrame(data, columns=['A', 'B'])
print(df)

So I have created this DataFrame from reading Fake Data.csv with columns A and B. For example, how would I retrieve the 5th value of column A so I can store it into the variable. How would I remove the 5th value of column A?

I tried the following code for my first question:

print(df.iat(4,0))

In theory, this should return the fifth value of column A, but I get the following error:

TypeError: '_AtIndexer' object is not callable

Before/after picture but the 5th value removed from column A needs to be stored in a variable first

  • 1
    Welcome to SO! What do you want to happen to the rest of the row in column B, or what should A be replaced with? Showing the output you want as well as whatever `Fake Data.csv` would be helpful. – ggorlen Jun 14 '20 at 18:32
  • Do you want the 5th value to be replaced for `null` or you want to delete the entire row? – vmouffron Jun 14 '20 at 18:35
  • @ggorlen Thanks! Let's say I remove the 5th value from column A, I would want all values following the 5th value to shift up (e.g. the 6th value becomes the 5th, etc etc). For simplicity, Fake Data.csv is just a table with columns A and B where column A has elements of all odd numbers less than 100, and column B has elements of all even numbers less than 100. – ephemeralhappiness Jun 14 '20 at 18:41
  • @vmouffron I would want to delete the entire row, but only after saving the value. – ephemeralhappiness Jun 14 '20 at 18:42
  • @ggorlen I updated my question with a before/after picture – ephemeralhappiness Jun 14 '20 at 18:54
  • [Text is better than pictures](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors), but thanks. – ggorlen Jun 14 '20 at 18:56
  • What should happen with the last value of column A, since that doesn't exist a row below it? – vmouffron Jun 14 '20 at 19:29

2 Answers2

0

Please use square brackets [ for iat statement. For example print(df.iat[4,0]).

  • This doesn't appear to answer the question. If you just want to comment on the author's code, please wait until you have 50 reputation. – ggorlen Jun 14 '20 at 18:57
  • @ggorlen Sir, not commenting, just pointing towards the source of the error. I hope that solved the author's problem. – Raza Cheema Jun 14 '20 at 19:04
  • I see, thanks. It doesn't remove the row, though, which is what OP wants: "I want all values following the 5th value to shift up (e.g. the 6th value becomes the 5th, etc etc". Granted, it's not obvious from the question itself. – ggorlen Jun 14 '20 at 19:26
0

If I understood correctly, this might work:

a = df.iloc[4, 0] df.iloc[4:,0] = df.iloc[4:,0].shift(-1)

vmouffron
  • 418
  • 1
  • 4
  • 11