0

import pandas as np from pandas import Series, DataFrame

dframe_final.to_csv('C:/Program Files/source/csv_data/2015/Merged files/jjj_all_merged.csv')

I have this part of my code and I need to add a new column at the end of this csv file and name it "New_name". And populate it based on different criteria:

For example, if cell1 is "a" and cell2 is "b" and cell3 is "1" and cell4 is "2 or 5" then enter "OK" If not, enter "NOT OK" or leave empty.

Column 1    Column 2    Column 3    Column 4    "New_name"
a              b           1           2            "OK"
a              b           1           5            "OK"
c              d           e           f          "NOT OK"

PLEASE HELP!!! :)

2
  • Read this 10 min introduction to pandas. It'll give you all the basic functions of pandas - pandas.pydata.org/pandas-docs/stable/10min.html Commented Dec 6, 2017 at 18:18
  • #Code below creates a new column in the aaa_all_merge file and add entries based on multiple criterias csv_input = np.read_csv('G:Flic_all_merged.csv') mask = (dframe_final['aaa'] == '159') & (dframe_final['bbb'].isin(['A','B','C'])) dframe_final['Final'] == np.where(mask,'OK',' not OK') This part doesn't work. I dont know why. Commented Dec 12, 2017 at 22:23

2 Answers 2

1

IIUC:

mask = (df['Column 1'] == 'a') & (df['Column 2'] == 'b') & (df['Column 3'] == '1') & (df['Column 4'].isin(['2','5']))
df['new_value'] = np.where(mask,'OK','NOT OK')

Output:

  Column 1 Column 2 Column 3 Column 4 "New_name" new_value
0        a        b        1        2       "OK"        OK
1        a        b        1        5       "OK"        OK
2        c        d        e        f   "NOT OK"    NOT OK
Sign up to request clarification or add additional context in comments.

14 Comments

@extrero01 if this solution helped yo would you consider accepting it.
I am having an error code on the last line. dframe_final["final'] = np.where(mask , 'ok' ,'not ok')
@extrero01 what is the error code you are getting?
G:\J\Python 2017>python_final.py sys:1: DtypeWarning: Columns (2,17,18,22,23,24,25) have mixed types. Specify dtype option on import or set low_memory=False. Traceback (most recent call last): File "C:\Program Files (x86)\Python\Python36-32\lib\site-packages\pandas\core\indexes\base.py", line 2522, in get_loc return self._engine.get_loc(key)
File "pandas_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Final_reserves'
|
0

In my case

with open('csv_file.csv', 'a', newline='') as csvfile:
                fwriter = csv.writer(csvfile, delimiter=',', quotechar='/', quoting=csv.QUOTE_MINIMAL)
                fwriter.writerow(List_to_add)

a in options means 'append'. Then I imported the csv file as 'pd.DataFrame'.

df = pd.read_csv('csv_file.csv')

I do not know if this is the best way.

import pandas as pd
import csv

You will need above two things.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.