1

I have CSV file Sales_In.csv and DataFrame is:

Sales_Region     Dollar_value
East             500
west             500
south            500
North            2000

I am Using pandas

import pandas as pd
import UUID

df= pd.read_csv('Sales_In.csv')
low_sales =df[(df['Dollar_value'] >=500) & (df['Dollar_value']<=1000)]

for index,row in low_sales.iterrows():
    for loop in range(10):
        print(uuid.uuid1().hex[:8],"REP"+str(uuid.uuid4().hex[:9]),row['Sales_Region'])

Above code gives me the output like below

279418fe HCP6eacac48a East
279418ff HCP6fb7d0ec2 East
27941900 HCP21cb84de3 East
27941901 HCP9b6a34bf0 East
27941902 HCP6aa9f0e20 East
27941903 HCP6fa5e3201 East
27941904 HCPabecf8c42 East
27941905 HCP0922c8acc East
27941906 HCPea9e91d7c East
27941907 HCP95f8dbfb9 East

I wanna write this to csv file with headers like below

CUS_KEY   Unique Key    Sales_Region
279418fe   HCP6eacac48a  East
279418ff   HCP6fb7d0ec2  East
27941900   HCP21cb84de3  East
...................

I am new to python I am kind of stuck in this help me out Thanks!

4
  • Where does Highly_low come from? And if you want to write to csv. Use the pd.DataFrame.to_csv() func! Commented Oct 19, 2018 at 7:14
  • Sorry that was a mistake I have modified my code. Commented Oct 19, 2018 at 7:16
  • What about using .. print(uuid.uuid1().hex[:8],"REP"+str(uuid.uuid4().hex[:9]),row['Sales_Region'][['CUS_KEY', 'Unique Key ' , 'Sales_Region']]) Commented Oct 19, 2018 at 7:17
  • @banpd, see above if that helps, However , you have shown only two columns in your sample? thats true.. Commented Oct 19, 2018 at 7:20

3 Answers 3

1

Use list comprehension for create tuples of values, create DataFrame by contructor and last write to file by to_csv:

low_sales =df[(df['Dollar_value'] >=500) & (df['Dollar_value']<=1000)]

L = [(uuid.uuid1().hex[:8],"REP"+str(uuid.uuid4().hex[:9]),x)
      for x in low_sales['Sales_Region'] for i in range(10)]

df = pd.DataFrame(L, columns=['CUS_KEY','Unique Key','Sales_Region'])
print (df.head(10))
    CUS_KEY    Unique Key Sales_Region
0  96cdb1f8  REPf2fedbce5         East
1  96cdb1f9  REPfc6d311f4         East
2  96cdb1fa  REPa31a28651         East
3  96cdb1fb  REP4f4689565         East
4  96cdb1fc  REP9e0a484a7         East
5  96cdb1fd  REPa8f763796         East
6  96cdb1fe  REP442ad19dd         East
7  96cdb1ff  REPa317fa7b0         East
8  96cdb200  REPb14ca95b9         East
9  96cdb201  REP60c31eb67         East

df.to_csv(file, index=False)

If want use your code:

L = []
for index,row in low_sales.iterrows():
    for loop in range(10):
        L.append((uuid.uuid1().hex[:8],"REP"+str(uuid.uuid4().hex[:9]),row['Sales_Region']))

df = pd.DataFrame(L, columns=['CUS_KEY','Unique Key','Sales_Region'])
Sign up to request clarification or add additional context in comments.

3 Comments

will it not give error TypeError: '>=' not supported between instances of 'str' and 'int' ? while using low_sales =df[(df['Dollar_value'] >=500) & (df['Dollar_value']<=1000)]
@pygo - what is your Dollar_value column ? There are numbers?
@ jezrael , yes, i'm just looking the data frame provided by op.
0

================================================================

For core python you can use the csv to append new row. make sure the file.csv will be in right folder

import csv
row = ['4', 'Anoop', 'Chamba']

with open('file.csv', 'a') as csvFile:
    writer = csv.writer(csvFile)
    writer.writerow(row)

csvFile.close()

======================================================================

In Pandas, below code should resolve your issue:-

df = pd.read_csv("myfile.csv")
df['new_column'] = 'some_value'
df.to_csv('myfile.csv')

Comments

0

I think, you need this:

df_2 = pd.DataFrame()
for loop in low_sales.Sales_Region:
    df_2 = df_2.append({'CUS_KEY':uuid.uuid1().hex[:8],
                        'Unique Key':"REP"+str(uuid.uuid4().hex[:9]),
                        'Sales_Region':loop},ignore_index=True)

then:

df_2.to_csv('New_df.csv', index=False, header=True)

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.