Home > database >  How do I combine columns (date, time, year) of a CSV file in the concat format (to create one new da
How do I combine columns (date, time, year) of a CSV file in the concat format (to create one new da

Time:10-18

I am trying to combine three columns from a CSV file which happen to be 'day', 'month', and 'year'. I want to combine them into one 'date' column and then delete them. However, I keep receiving error messages. This is what I have so far:

import pandas as pd 
def airport_data(cur_file, airport_code):
    
    #import data 
    airportsearch = pd.read_csv('/work/Data.airports.csv')
    
    #drop all rows of data that do not belong to the desired airport code 
    airportsearch.dropna([lambda x:
        (x['origin_airport'].isin(['SFO'])) &
        (x)['destination_airport'].isin (['SFO'])]

    #make columns lower case
    airportsearch.columns = airportsearch.columns.str.lower()

    #create new column 
    df = pd.DataFrame(airportsearch, columns=["year", "month", "day"])
    df.dates = pd.to_datetime(df.Year) 

    #drop old columns 
    airportsearch.drop(columns=['day', 'month', 'year'], axis=1)

The error that I am getting reads: 'Dataframe' object has no attribute 'Year'. I am unsure what this even means. Any help would be appreciated! Here is the report of the error in case that helps

report of error message

CodePudding user response:

The error message you have linked is that "Year" is not a valid attribute of your DataFrame. Python is case-sensitive, and from your code, it seems you've called that column "year".

Also, please cut & paste error messages as text rather than using screenshots on StackOverflow

  • Related