Reputation: 267
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:
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
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