我有以下代码:
businessdata = ['Name of Location','Address','City','Zip Code','Website','Yelp',
'# Reviews', 'Yelp Rating Stars','BarRestStore','Category',
'Price Range','Alcohol','Ambience','Latitude','Longitude']
business = pd.read_table('FL_Yelp_Data_v2.csv', sep=',', header=1, names=businessdata)
print '\n\nBusiness\n'
print business[:6]
Run Code Online (Sandbox Code Playgroud)
它读取我的文件并创建一个我可以使用的 Panda 表。我需要的是计算“类别”变量的每一行中有多少类别,并将此数字存储在名为“#类别”的新列中。这是目标列示例:
Category
French
Adult Entertainment , Lounges , Music Venues
American (New) , Steakhouses
American (New) , Beer, Wine & Spirits , Gastropubs
Chicken Wings , Sports Bars , American (New)
Japanese
Run Code Online (Sandbox Code Playgroud)
期望的输出:
Category # Categories
French 1
Adult Entertainment , Lounges , Music Venues 3
American (New) , Steakhouses 2
American (New) , Beer, Wine & Spirits , Gastropubs 4
Chicken Wings , Sports Bars , American (New) 3
Japanese 1
Run Code Online (Sandbox Code Playgroud)
编辑 1:
原始输入 = CSV 文件。目标列:“类别”我还不能发布屏幕截图。我不认为要计算的值是列表。
这是我的代码:
business = pd.read_table('FL_Yelp_Data_v2.csv', sep=',', header=1, names=businessdata, skip_blank_lines=True)
#business = pd.read_csv('FL_Yelp_Data_v2.csv')
business['Category'].str.split(',').apply(len)
#not sure where to declare the df part in the suggestions that use it.
print business[:6]
Run Code Online (Sandbox Code Playgroud)
但我不断收到以下错误:
TypeError: object of type 'float' has no len()
Run Code Online (Sandbox Code Playgroud)
编辑2:
我放弃。感谢您的所有帮助,但我必须想办法解决其他问题。
使用 pd.read_csv 使输入更容易:
business = pd.read_csv('FL_Yelp_Data_v2.csv')
Run Code Online (Sandbox Code Playgroud)
http://pandas.pydata.org/pandas-docs/stable/ generated/pandas.read_csv.html
创建后,您可以创建一个函数来按“,”分割类别列,并计算结果列表的长度。使用 lambda 并应用。
| 归档时间: |
|
| 查看次数: |
7762 次 |
| 最近记录: |