-1

I have a function that takes the path to CSV file as an argument and adds a column:

def df_change(path):
    pd.read_csv(path)
    df55 = pd.read_csv(path)
    df55['name'] = 'xxx'
    print(df55)

But the result I get is Series:

enter image description here

But I need it to be a Dataframe.

I tried to store the result for converting by using this method:

enter image description here

But it didn't work.

I also tried .to_frame(), but I either used it wrong or it can't be applied in my case

Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
salehelas
  • 1
  • 2
  • 2
    What do you mean by "the result I get is Series"? The result in your first image looks very much like a DataFrame with four columns. – mcsoini Nov 13 '21 at 11:54
  • I agree with @mcsoini, you have a DataFrame, not a Series. I'm wondering if the confusion is stemming from the fact that you read the csv, create a dataframe, and after adding the column, do absolutely nothing with the dataframe (like return it, or write it back to file). – Riley Nov 13 '21 at 11:58
  • I am new to Python but correct me if I'm wrong. From my understanding, Dataframe looks differently (unfortunately I can't add screenshots here in the comments), in Jupyter Notebook it has visual divides of the rows and bold column names, my result doesn't have these things. And I can't use methods like append and others with this type of data. – salehelas Nov 13 '21 at 12:05

1 Answers1

0

I found the answer: I had to use return instead of print() at the end of my function:

def df_change(path):
    pd.read_csv(path)
    df55 = pd.read_csv(path)
    df55['name'] = 'xxx'
    return df55

With the return in my function the following method of storing the result works:

def myfunction():
    value = "myvalue"
    return value

var = myfunction()
print(var)
salehelas
  • 1
  • 2