-2

I am trying to open a text file using pandas in Jupyter Notebook. Then iterating through the file, and search and find specific piece of data. Then save the rows of the searched data to a new file.

What I need to do:

  1. Open text file using pandas in Jupyter Notebook
  2. Iterate through the file, searching for "Richmond Group" data
  3. Save Only the new search information to a new file

I can not get passed the error to finish the code. I am struggling to open the file and finding the search data. Please help me.

MY CODE:

import pandas as df

df=pd.read_csv('F:/Wells FargoZinitra.txt', 'r')

print(df)

Error:

enter image description here

nucsit026
  • 652
  • 7
  • 16
Frank
  • 13
  • 1
  • 4
  • 12

2 Answers2

2

To read data from CSV file:

import pandas as pd
#df = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
#put 'r' before the path string to address any special characters in the path.
df = pd.read_csv(r'F:/Wells FargoZinitra.csv')  
print (df) #df is <class 'pandas.core.frame.DataFrame'>

To save data in CSV file:

#df.to_csv(r'Path where you want to store the CSV file\File name.csv',sep =",",index = True)
df.to_csv(r'F:/Wells FargoZinitra2.csv')

To select rows from a dataframe:

You can check the answers of this question and select which condition is suitable to your case.

nucsit026
  • 652
  • 7
  • 16
  • I am not sure I understand the answer, let try my problem with you. I have a file with about 1,000 rows and 4 columns. Columns are "Product", "Cost", "Quantity", "Purchase" & "Customer" – Frank Jan 10 '20 at 21:38
  • I am looking to find a product bought by a customer and find the total amount bought by that costumer. Then create a file for that costumer for auditing. – Frank Jan 10 '20 at 21:42
  • 1
    To get help, I advise you to divide your problem to sub objectives, for example : How to read CSV file in pandas, How to find a product bought by a customer (_How to get column's value/s based on row value_). How to find the total amount bought of a customer.(_How to sum values in a column based on row value_), How to save CSV file in pandas. Then try to solve it one by one, and post the programming problems which faced you. – nucsit026 Jan 11 '20 at 19:03
0

1.) Pandas are just in memory tables and it needs to be in a specific format.

2.) If it's a flat file( like csv), try Saving it with that extension

Below is how you do it

import pandas as pd

df=pd.read_csv('F:/Wells FargoZinitra.csv')

print(df) 

Either change it to csv format or use a delimiter in it.

This link might prove to be helpful for you : https://www.learnpython.org/en/Pandas_Basics

If you're new to python, I'd suggest you start from the basics.

High-Octane
  • 1,104
  • 5
  • 19