0

I have attached a screenshot of my excel sheet. I want to store the length of every string in SUPPLIER_id Length column. But when I run my code, CSV columns are blanks.

And when I use this same code in different CSV, it works well.

I am using following code but not able to print the data.

I have attached the snippet of csv. Can somebody tell me why is this happening:

enter image description here

import pandas as pd    
data = pd.read_csv(r'C:/Users/patesari/Desktop/python work/nba.csv')    
df = pd.DataFrame(data, columns= ['SUPPLIER_ID','ACTION'])    
data.dropna(inplace = True)    
data['SUPPLIER_ID']= data['SUPPLIER_ID'].astype(str)    
data['SUPPLIER_ID LENGTH']= data['SUPPLIER_ID'].str.len()    
data['SUPPLIER_ID']= data['SUPPLIER_ID'].astype(float)    
data    
print(df)    
data.to_csv("C:/Users/patesari/Desktop/python work/nba.csv")
6
  • 1
    Of course it would be empty because you are not doing anything with df. Commented Jun 20, 2019 at 7:42
  • (also please put code in codeblocks, here's how to do it, you can always check the little help button at the top right of your text box) Commented Jun 20, 2019 at 7:45
  • @amanb But he is saving the data variable and not the df. It should have worked. Don't you think the same? Commented Jun 20, 2019 at 7:45
  • @AmazingThingsAroundYou, a few problems with the code: 1) df is unnecessary 2) Type casting 'SUPPLIER_ID' to str first and then again to float doesn't make sense. 3) Reading from and writing to the same csv 4) We cannot be sure if the csv will have any data in the end, without looking at the actual data. Commented Jun 20, 2019 at 7:51
  • i have attached the snippet of actual data. Commented Jun 20, 2019 at 9:06

2 Answers 2

1

I faced a similar problem in the past.

Instead of:

df = pd.DataFrame(data, columns= ['SUPPLIER_ID','ACTION']) 

Type this:

data.columns=['SUPPLIER_ID','ACTION']

Also, I don't understand why did you create DataFrame df. It was unnecessary in my opinion.

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

Comments

0

Aren't you getting a SettingWithCopyWarning from pandas? I would imagine (haven't ran this code) that these lines

data['SUPPLIER_ID']= data['SUPPLIER_ID'].astype(str)
data['SUPPLIER_ID LENGTH']= data['SUPPLIER_ID'].str.len()    
data['SUPPLIER_ID']= data['SUPPLIER_ID'].astype(float)  

would not do anything, and should be replaced with

data.loc[:, 'SUPPLIER_ID']= data['SUPPLIER_ID'].astype(str)  
data.loc[:, 'SUPPLIER_ID LENGTH']= data['SUPPLIER_ID'].str.len()    
data.loc[:, 'SUPPLIER_ID']= data['SUPPLIER_ID'].astype(float)  

4 Comments

I am not getting any error,just my csv goes blank when i run the code
I tried your code snippet as well but its not working for me. Y it is so, that the same code is working for other csv files but not for other.
Are you sure the original csv had data in it? you're reading and writing to the same file, if you've done any mistake the data might have been erased, if this code works for other csv files it should work on this one as well.
yes.when i am reading my csv(instead to giving specific column name) as whole., few columns are gets erased.,i think that can be the issue.

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.