-2

I Have the code below how do I save the df file as csv and how do i get it back in another program,and i want to extract each row element , is it possible to extract each element population when the corresponding date in entered

    import numpy as np
    from datetime import datetime,timedelta
    import pandas as pd

    # make your data a frame
    df = pd.DataFrame([[2020,    713000], 
    [    2019,    703000], 
    [    2018,    694000], 
    [    2017,    684000], 
    [    2016,    674000], 
    [    2015,    664000], 
    [    2014,    655000], 
    [    2013,    645000], 
    [    2012,    636000], 
    [    2011,    627000]], columns=['DateTime','pop'])

    # make DateTime column an datetime object
    df['DateTime'] = df['DateTime'].apply(lambda x: datetime(x,1,1))

    # create a time range for each day in your period
    time_range = np.arange(datetime(2011, 1,1), datetime(2021,1,1), timedelta(days=1))

   # make time_range a frame 
   af = pd.DataFrame(time_range, columns=['DateTime'])

   # merge both together (left join on column DateTime) and interpolate the gaps
   df = af.merge(df, on='DateTime', how='left').interpolate()

   print(df)

I get the output

     DateTime            pop
    0    2011-01-01  627000.000000
    1    2011-01-02  627024.657534
    2    2011-01-03  627049.315068
    3    2011-01-04  627073.972603
    4    2011-01-05  627098.630137
    ...         ...            ...
    3648 2020-12-27  713000.000000
    3649 2020-12-28  713000.000000
    3650 2020-12-29  713000.000000
    3651 2020-12-30  713000.000000
    3652 2020-12-31  713000.000000
  • `df.to_csv()`, https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html – Ben Pap Sep 11 '20 at 16:27
  • 1
    Searching for `pandas csv` on StackOverflow alone returned 36,080 results. – mac13k Sep 11 '20 at 16:54
  • Does this answer your question? [Writing a pandas DataFrame to CSV file](https://stackoverflow.com/questions/16923281/writing-a-pandas-dataframe-to-csv-file) – ifly6 Sep 11 '20 at 18:02

1 Answers1

-1
df.to_csv("my_csvfilename.csv")

Here's the link to the documentation so you can decide whether you want to use any of the other control parameters.

Thomas Kimber
  • 7,741
  • 2
  • 19
  • 33