SLglider
SLglider

Reputation: 267

How to parse pandas dataframes between functions

I want to read in data from a csv file into a pandas dataframe. Then I want to do several operations on this dataframe. I want to do this in different functions (ideally in a separate file).

import pandas as pd

def read_text(file):
    df = pd.read_csv(file,skipinitialspace=True, sep=";", encoding = "ISO-8859-1")
    return [df]


file = "/path/file.txt"
content = pd.DataFrame()
content = read_text(file)

Now, the reading of the file works fine. But "content" does not seem to be a dataframe anymore. At least, if I try something like e.g. print (content.value) there does not seem to be this option. What I later want to do is:

  1. send dataframe to a function to remove duplicates and return dataframe
  2. Take this new dataframe and remove certain entries and again return this dataframe
  3. Do some more things with the dataframe

Ideally, these functions will be in a separate file. But I will take care of this later. For right now it would be of great help, if I could parse these dataframes back and forth.

Upvotes: 1

Views: 1213

Answers (1)

Julien Marrec
Julien Marrec

Reputation: 11895

You're returning [df] so that's a list of a single dataframe. You should modify your code as follows:

import pandas as pd

def read_text(file):
    df = pd.read_csv(file,skipinitialspace=True, sep=";", encoding = "ISO-8859-1")
    return df


file = "/path/file.txt"
content = read_text(file)

Upvotes: 1

Related Questions