Problem on how to format my data using pandas.loc() function

Recently I asked this: How to manipulate data in a .csv file using Pandas and access specific row and column? question on how to parse some data and users recommended the pd.loc() function. My .csv file looks like that

                                       entry  origin  payment_type     x1                                           NaN           NaN     x2                                           NaN           NaN     x3                                           NaN           NaN     x4                                           NaN           NaN     x5                                           NaN           NaN 

Using this script

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.loc[[row], ["payment_type"]] = "sample"     if row[27] == "Y":         data.loc[[row], ["payment_type"]] = "Check Card"      if row[37] == "Y":         data.loc[row, "origin"] = "ex1"     else:         data.loc[row, "origin"] = "ex2"  data.to_csv("exe.csv") 

And I get as an output at exe.csv file this:

enter image description here

How is it possible to format my data as in example.csv file but instead of NaN to have the data I parsed?

For example, I would like to format my data like that:

enter image description here

Any help would be appreciated.

Thank you in advance

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.