使用Pandas从CSV中提取列

时间:2018-06-05 04:24:35

标签: python pandas csv

我正在尝试从csv文件中提取Start Station,下面是示例数据。

Start Time,End Time,Trip Duration,Start Station,End Station,User Type,Gender,Birth Year

1423854,2017-06-23 15:09:32,2017-06-23 15:14:53,321,Wood St & Hubbard St,Damen Ave & Chicago Ave,Subscriber,Male,1992.0

我遇到的问题是当我尝试提取数据时收到以下错误消息:

  

AttributeError:'Series'对象没有属性'start'

def load_data(city, month, day):

# load data file into a dataframe
df = pd.read_csv(CITY_DATA[city])

我认为我的问题源于转换启动站,但似乎无法解释原因。

# convert the Start Station column to dataframe
df['Start Station'] = pd.DataFrame(df['Start Station'])

# extract street names from Start Station and End Station to create new columns
df['start'] = df['Start Station'].start

def station_stats(df):
"""Displays statistics on the most popular stations and trip."""

# TO DO: display most commonly used start station
popular_start_station = df['start']
print(popular_start_station)

1 个答案:

答案 0 :(得分:0)

您的代码令人困惑。试试这个:

df = pd.read_csv(CITY_DATA, index = True) # load data file into a one df
start_data_series = df[['Start Station']] # create series with column of interest

您可以根据自己的喜好在第二行添加更多列。如需进一步阅读,请参阅this发布。