简体   繁体   中英

How to manipulate data in a .csv file using Pandas and access specific row and column?

I have a.csv file that contains four columns and about 700000 rows. My problem is I can't access a specific cell, but only the whole row. My code:

import pandas as pd

data = pd.read_csv("example.csv")
entries = data["entry"].astype(str)
payments = data["payment_type"].astype(str)
origins = data["origin"].astype(str)


for row in entries:
    if row[26] == "Y":
        data["payment_type"] = "sample"
    if row[27] == "Y":
        data["payment_type"] = "Check Card"
    ...

Eg In the first row of the.csv file, I want to format the cell in the "origin" column, according to the "entry" column of the same row. The script does this, but as written now, it formats the whole column according to what the value of the last entry is. I think my problem is in the "for" loop, on how to access the specific row of a column.

Any help would be appreciated.

Thank you in advance.

you could use np.where function and define rules when to format rows that match it. Or if you have multiple rules and multiple conditions, you could use np.select .

You are replacing the whole column, you have to add the row for each column

import pandas as pd

data = pd.read_csv("example.csv")
entries = data["entry"].astype(str)
payments = data["payment_type"].astype(str)
origins = data["origin"].astype(str)


for row in entries:
    if row[26] == "Y":
        data["payment_type"][row] = "sample"
    if row[27] == "Y":
        data["payment_type"][row] = "Check Card"
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM