[英]Extracting columns from CSV using Pandas
I am trying to extract the Start Station from a csv file, example data below. 我正在尝试从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
The problem I am having is when I try to extract the data I receive the following error message: 我遇到的问题是当我尝试提取数据时收到以下错误消息:
AttributeError: 'Series' object has no attribute 'start'
AttributeError:“系列”对象没有属性“开始”
def load_data(city, month, day):
# load data file into a dataframe
df = pd.read_csv(CITY_DATA[city])
I believe my problem stems from converting the Start Station, but can't seem to figure why. 我相信我的问题源于转换Start Station,但似乎无法弄清楚原因。
# 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)
Your code is confusing. 您的代码令人困惑。 Just try this:
尝试一下:
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
You can add more columns to the second line according to your liking. 您可以根据自己的喜好在第二行中添加更多列。 For further reading, refer to this post.
有关更多阅读,请参阅此文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.