我有csv
喜欢
PassengerId,Survived,Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked
1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S
2,1,1,"Cumings, Mrs. John Bradley (Florence Briggs Thayer)",female,38,1,0,PC 17599,71.2833,C85,C
3,1,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2. 3101282,7.925,,S
4,1,1,"Futrelle, Mrs. Jacques Heath (Lily May Peel)",female,35,1,0,113803,53.1,C123,S
5,0,3,"Allen, Mr. William Henry",male,35,0,0,373450,8.05,,S
6,0,3,"Moran, Mr. James",male,,0,0,330877,8.4583,,Q
7,0,1,"McCarthy, Mr. Timothy J",male,54,0,0,17463,51.8625,E46,S
8,0,3,"Palsson, Master. Gosta Leonard",male,2,3,1,349909,21.075,,S
我需要算上最受欢迎的男女名字。 我可以像
那样做for names in data['Name']:
name = names.split(', ')
print name[0]
但是,有没有办法只使用pandas
?
答案 0 :(得分:3)
我认为您可以首先使用split
将名称解析为新的Series
ser
,然后使用Sex
Ser
将print data
Name Sex Age SibSp \
0 Braund, Mr. Owen Harris male 22.0 1
1 Futrelle, Mrs. John Bradley (Florence Briggs T... female 38.0 1
2 Heikkinen, Miss. Laina female 26.0 0
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1
4 Allen, Mr. William Henry male 35.0 0
5 Moran, Mr. James male NaN 0
6 McCarthy, Mr. Timothy J male 54.0 0
7 Braund, Master. Gosta Leonard male 2.0 3
Parch Ticket Fare Cabin Embarked
0 0 A/5 21171 7.2500 NaN S
1 0 PC 17599 71.2833 C85 C
2 0 STON/O2. 3101282 7.9250 NaN S
3 0 113803 53.1000 C123 S
4 0 373450 8.0500 NaN S
5 0 330877 8.4583 NaN Q
6 0 17463 51.8625 E46 S
7 1 349909 21.0750 NaN S
与ser = data['Name'].str.split(',').str[0]
print ser
0 Braund
1 Futrelle
2 Heikkinen
3 Futrelle
4 Allen
5 Moran
6 McCarthy
7 Braund
Name: Name, dtype: object
print ser.groupby([data['Sex'], ser]).count()
Sex Name
female Futrelle 2
Heikkinen 1
male Allen 1
Braund 2
McCarthy 1
Moran 1
dtype: int64
print ser.groupby([data['Sex'], ser]).count().nlargest(4)
Sex Name
female Futrelle 2
male Braund 2
female Heikkinen 1
male Allen 1
dtype: int64
解析为all_names
{3}}和groupby
:
data['all_names'] = data['Name'].str.split(',').str[0]
print data
Name Sex Age SibSp \
0 Braund, Mr. Owen Harris male 22.0 1
1 Futrelle, Mrs. John Bradley (Florence Briggs T... female 38.0 1
2 Heikkinen, Miss. Laina female 26.0 0
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1
4 Allen, Mr. William Henry male 35.0 0
5 Moran, Mr. James male NaN 0
6 McCarthy, Mr. Timothy J male 54.0 0
7 Braund, Master. Gosta Leonard male 2.0 3
Parch Ticket Fare Cabin Embarked all_names
0 0 A/5 21171 7.2500 NaN S Braund
1 0 PC 17599 71.2833 C85 C Futrelle
2 0 STON/O2. 3101282 7.9250 NaN S Heikkinen
3 0 113803 53.1000 C123 S Futrelle
4 0 373450 8.0500 NaN S Allen
5 0 330877 8.4583 NaN Q Moran
6 0 17463 51.8625 E46 S McCarthy
7 1 349909 21.0750 NaN S Braund
print data.groupby(['Sex', 'all_names'])['all_names'].count()
Sex all_names
female Futrelle 2
Heikkinen 1
male Allen 1
Braund 2
McCarthy 1
Moran 1
Name: all_names, dtype: int64
print data.groupby(['Sex', 'all_names'])['all_names'].count().nlargest(4)
Sex all_names
female Futrelle 2
male Braund 2
female Heikkinen 1
male Allen 1
Name: all_names, dtype: int64
与使用辅助列super(int, String)
super(int)
super() // for completeness
相同:
AttributeError at /books/
type object 'Books' has no attribute 'Objects'
Request Method: GET
Request URL: http://127.0.0.1:8000/books/
Django Version: 1.9
Exception Type: AttributeError
Exception Value:
type object 'Books' has no attribute 'Objects'
Exception Location: F:\FYP\db2\testproject\books\views.py in index, line 7
Python Executable: C:\Python27\python.exe
Python Version: 2.7.8
Python Path:
['F:\\FYP\\db2\\testproject',
'C:\\Python27\\lib\\site-packages\\setuptools-7.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\networkx-1.9.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\decorator-3.4.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\sys-1.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\requests-2.5.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\beautifulsoup-3.2.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\beautifulsoup4-4.3.2-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\nltk-3.0.1-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\lxml-3.4.1-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\stripogram-1.5-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\html2text-2014.12.29-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\dom-0.6-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\termcolor-1.1.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\simplejson-3.6.5-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\xmltodict-0.9.2-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\mysql-0.0.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.5-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\textstat-0.1.6-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\utils-0.7-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\tweetpy-0.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\requests_oauthlib-0.5.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\oauthlib-0.7.2-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\tweepy-3.3.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\six-1.9.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\cython-0.22-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\numpy-1.9.2-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\pytz-2015.4-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\python_dateutil-2.4.2-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pandas-0.16.1-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\pip-7.0.3-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\matplotlib-1.4.3-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\pyparsing-2.0.3-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\html5lib-1.0b8-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pelican-3.6.3-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\unidecode-0.04.19-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\blinker-1.4-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\docutils-0.12-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pygments-2.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\jinja2-2.8-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\feedgenerator-1.7-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\markupsafe-0.23-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\mechanize-0.2.5-py2.7.egg',
'C:\\Windows\\SYSTEM32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time: Fri, 29 Apr 2016 11:26:32 +0000
from django.db import models
# Create your models here.
class Books(models.Model):
title = models.CharField(max_length=150)
author = models.CharField(max_length=100)
read = models.CharField(max_length=3)
def _unicode_(self):
return self.title + "/"+ self.author+ "/" + self.read