0

I have my algorithm and code. It works but writes the dataframe to an entire column in my output CSV file. I want to write the dataframe to only rows where the column is equal to a certain value. This is on Line 6. Rest of logic, syntax seems fine. I researched a fair few options including sort_values(), apply(), map(), lambda, writing if statements. It seems like an easy solution but I cannot get to write the dataframe to specific rows and column combination. If this question has been asked for before, please refer me to the solution.

Thank you.

Output file - Line 1

df_Working_File = pd.read_csv('Working\WF.csv')

Input File - Line 2

df_GSTRemoved = pd.read_csv('Working\GSTRemoved.csv')

Concat for vlookup essentially in output file - Line 3

df_OriginalKey = df_GSTRemoved.apply(lambda x:x['Origin'] + x['Destination'],1)

Concat for vlookup in input file - Line 4

df_Key = df_Working_File.apply(lambda x:x['Origin'] + x['Destination'],1)

Value of column dataset to write - Line 5

df_Charge_Per_Item = df_GSTRemoved['500g']

Line 6 - I want to write the dataframe to column 9 for all rows where Origin is GL in the input file

df_Working_File.apply(df_Charge_Per_Item, df_Working_File['Origin'] == 'GL')

Line 7 - Writing to csv with no index column

df_Working_File.to_csv("Working/writetoCSV.csv", index=False)

1
  • 1
    Can you add sample data - 5, 6 rows with desired output? Commented May 18, 2017 at 8:15

1 Answer 1

1

You can write subsets of the dataframe by using boolean vactors forexample:

# boolean vector:
selector = df_Working_File['Origin'] == 'GL'
# write only rows to csv where the vector is true:
df_Working_File[selector].to_csv("Working/writetoCSV.csv", index=False)

If you also want to write only certain columns, you can use this:

df_Working_File[selector][['col1', 'col2']].to_csv("Working/writetoCSV.csv", index=False)

Btw you can concat columns also like this:

df_OriginalKey = df_GSTRemoved.Origin + df_GSTRemoved.Destination

edit

If you want to change/edit only rows that meet certain conditions you can do this:

 selector = df_Working_File['Origin'] == 'GL'
 df_Working_File.ix[selector, 'column_to_change'] = 'changed value'

I hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for concat. That wrote only the rows where Origin is GL and removed all other rows where Origin is not GL. I need the other Origins to be in the file and to write to only rows where Origin is GL. # Input file Origin Destination Zone 500g GL N1 Sydney 7.563636364 GL GF Gosford 7.563636364 #Output File Format only header is provided Origin Destination Charge (per item) AAT, AAT, GL, AAT`` GL, N1` GL, GF GL, WG GL, NC GL, CB The issue is that I am not able to write to the rows where Origin is GL.

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.