0

I am using pandas to import a csv file with stocks data. It seems like every time I save the file, Pandas adds a row with the id of the row to the file.

import yfinance as yf
import pandas as pd

#Open File
df = pd.read_csv('balance.csv')

#Calculating Current Value
df['Value'] = df['BuyPrice'] * df['BuyAmount']

#Adding Most Recent Price to CSV File
ticker_stock = yf.Ticker('AAPL')
data = ticker_stock.history()
df['Price'] = (data.tail(1)['Close'].iloc[0])
print(df)

#Save File
df.to_csv('balance.csv')

If I run the program 3 times, I will have 3 rows with ids in them. Is there a way to stop pandas from showing me the row id and adding it to the file each time I save it? It also seems like the id is stopping me from using the data in the table to do statistics since it also includes the id every time I pull data from the table.

Louis
  • 187
  • 1
  • 3
  • 15
  • 1
    First step is to read the help / docs: [to_csv](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html). Just use `index=False`. – tdelaney Oct 11 '20 at 02:03
  • This worked for saving the table. Can I also use this on read_csv? I don't want to see the index when I print the table – Louis Oct 11 '20 at 02:13
  • @Louis perhaps this helps https://stackoverflow.com/questions/20107570/removing-index-column-in-pandas-when-reading-a-csv – Miguel Trejo Oct 11 '20 at 02:27
  • @Louis - You have a couple of options in `[read_csv](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html#pandas-read-csv). `index_col=0` will use column zero as the index. If you know the number of columns that should be there, lets say 3, `usecols=[1,2,3]` will skip col zero index. – tdelaney Oct 11 '20 at 02:38
  • If you have some files with the index and some without because of previous `to_csv`, the fist column will not have a label. You could do `if "Unamed: 0" in df: df.drop(columns=["Unamed: 0"], inplace=True)` – tdelaney Oct 11 '20 at 02:41
  • I am now able to use my own index from the csv file, but pandas still prints the index. Lets say I write ```df = pd.read_csv('balance.csv', index_col=0) print(df['Ticker'])``` It still outputs ```0 AAPL``` and not just ```AAPL``` – Louis Oct 11 '20 at 11:48

0 Answers0