import numpy as np
import pandas as pd
df = pd. read_csv( r'C:\Users\lenovo\Desktop\train.csv' )
df. head( 3 )
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked 0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S 1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C 2 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S
chunker = pd. read_csv( r'C:\Users\lenovo\Desktop\train.csv' , chunksize= 1000 )
df = pd. read_csv( r'C:\Users\lenovo\Desktop\train.csv' , names= [ '乘客ID' , '是否幸存' , '仓位等级' , '姓名' , '性别' , '年龄' , '兄弟姐妹个数' , '父母子女个数' , '船票信息' , '票价' , '客舱' , '登船港口' ] , index_col= '乘客ID' , header= 0 )
df. head( )
是否幸存 仓位等级 姓名 性别 年龄 兄弟姐妹个数 父母子女个数 船票信息 票价 客舱 登船港口 乘客ID 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 S 5 0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S
df. info( )
<class 'pandas.core.frame.DataFrame'>
Int64Index: 891 entries, 1 to 891
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 是否幸存 891 non-null int64
1 仓位等级 891 non-null int64
2 姓名 891 non-null object
3 性别 891 non-null object
4 年龄 714 non-null float64
5 兄弟姐妹个数 891 non-null int64
6 父母子女个数 891 non-null int64
7 船票信息 891 non-null object
8 票价 891 non-null float64
9 客舱 204 non-null object
10 登船港口 889 non-null object
dtypes: float64(2), int64(4), object(5)
memory usage: 83.5+ KB
df. head( 10 )
是否幸存 仓位等级 姓名 性别 年龄 兄弟姐妹个数 父母子女个数 船票信息 票价 客舱 登船港口 乘客ID 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 S 5 0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S 6 0 3 Moran, Mr. James male NaN 0 0 330877 8.4583 NaN Q 7 0 1 McCarthy, Mr. Timothy J male 54.0 0 0 17463 51.8625 E46 S 8 0 3 Palsson, Master. Gosta Leonard male 2.0 3 1 349909 21.0750 NaN S 9 1 3 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) female 27.0 0 2 347742 11.1333 NaN S 10 1 2 Nasser, Mrs. Nicholas (Adele Achem) female 14.0 1 0 237736 30.0708 NaN C
df. tail( 15 )
是否幸存 仓位等级 姓名 性别 年龄 兄弟姐妹个数 父母子女个数 船票信息 票价 客舱 登船港口 乘客ID 877 0 3 Gustafsson, Mr. Alfred Ossian male 20.0 0 0 7534 9.8458 NaN S 878 0 3 Petroff, Mr. Nedelio male 19.0 0 0 349212 7.8958 NaN S 879 0 3 Laleff, Mr. Kristo male NaN 0 0 349217 7.8958 NaN S 880 1 1 Potter, Mrs. Thomas Jr (Lily Alexenia Wilson) female 56.0 0 1 11767 83.1583 C50 C 881 1 2 Shelley, Mrs. William (Imanita Parrish Hall) female 25.0 0 1 230433 26.0000 NaN S 882 0 3 Markun, Mr. Johann male 33.0 0 0 349257 7.8958 NaN S 883 0 3 Dahlberg, Miss. Gerda Ulrika female 22.0 0 0 7552 10.5167 NaN S 884 0 2 Banfield, Mr. Frederick James male 28.0 0 0 C.A./SOTON 34068 10.5000 NaN S 885 0 3 Sutehall, Mr. Henry Jr male 25.0 0 0 SOTON/OQ 392076 7.0500 NaN S 886 0 3 Rice, Mrs. William (Margaret Norton) female 39.0 0 5 382652 29.1250 NaN Q 887 0 2 Montvila, Rev. Juozas male 27.0 0 0 211536 13.0000 NaN S 888 1 1 Graham, Miss. Margaret Edith female 19.0 0 0 112053 30.0000 B42 S 889 0 3 Johnston, Miss. Catherine Helen "Carrie" female NaN 1 2 W./C. 6607 23.4500 NaN S 890 1 1 Behr, Mr. Karl Howell male 26.0 0 0 111369 30.0000 C148 C 891 0 3 Dooley, Mr. Patrick male 32.0 0 0 370376 7.7500 NaN Q
df. isnull( ) . head( )
是否幸存 仓位等级 姓名 性别 年龄 兄弟姐妹个数 父母子女个数 船票信息 票价 客舱 登船港口 乘客ID 1 False False False False False False False False False True False 2 False False False False False False False False False False False 3 False False False False False False False False False True False 4 False False False False False False False False False False False 5 False False False False False False False False False True False
df. to_csv( 'train_chinese.csv' )
text = pd. read_csv( r'C:\Users\lenovo\Desktop\train_chinese.csv' )
text. head( )
乘客ID 是否幸存 仓位等级 姓名 性别 年龄 兄弟姐妹个数 父母子女个数 船票信息 票价 客舱 登船港口 0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S 1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C 2 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S 3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 S 4 5 0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S
frame = pd. DataFrame( np. arange( 8 ) . reshape( ( 2 , 4 ) ) ,
index= [ '2' , '1' ] ,
columns= [ 'd' , 'a' , 'b' , 'c' ] )
frame
frame. sort_index( )
frame. sort_index( axis= 1 )
frame. sort_index( axis= 1 , ascending= False )
frame. sort_values( by= [ 'a' , 'c' ] )
text. sort_values( by= [ '票价' , '年龄' ] , ascending= False ) . head( 3 )
乘客ID 是否幸存 仓位等级 姓名 性别 年龄 兄弟姐妹个数 父母子女个数 船票信息 票价 客舱 登船港口 679 680 1 1 Cardeza, Mr. Thomas Drake Martinez male 36.0 0 1 PC 17755 512.3292 B51 B53 B55 C 258 259 1 1 Ward, Miss. Anna female 35.0 0 0 PC 17755 512.3292 NaN C 737 738 1 1 Lesurer, Mr. Gustave J male 35.0 0 0 PC 17755 512.3292 B101 C
frame1_a = pd. DataFrame( np. arange( 9 . ) . reshape( 3 , 3 ) ,
columns= [ 'a' , 'b' , 'c' ] ,
index= [ 'one' , 'two' , 'three' ] )
frame1_b = pd. DataFrame( np. arange( 12 . ) . reshape( 4 , 3 ) ,
columns= [ 'a' , 'e' , 'c' ] ,
index= [ 'first' , 'one' , 'two' , 'second' ] )
frame1_a
a b c one 0.0 1.0 2.0 two 3.0 4.0 5.0 three 6.0 7.0 8.0
frame1_b
a e c first 0.0 1.0 2.0 one 3.0 4.0 5.0 two 6.0 7.0 8.0 second 9.0 10.0 11.0
frame1_a + frame1_b
a b c e first NaN NaN NaN NaN one 3.0 NaN 7.0 NaN second NaN NaN NaN NaN three NaN NaN NaN NaN two 9.0 NaN 13.0 NaN
max ( text[ '兄弟姐妹个数' ] + text[ '父母子女个数' ] )
10
frame2 = pd. DataFrame( [ [ 1.4 , np. nan] ,
[ 7.1 , - 4.5 ] ,
[ np. nan, np. nan] ,
[ 0.75 , - 1.3 ]
] , index= [ 'a' , 'b' , 'c' , 'd' ] , columns= [ 'one' , 'two' ] )
frame2
one two a 1.40 NaN b 7.10 -4.5 c NaN NaN d 0.75 -1.3
frame2. describe( )
one two count 3.000000 2.000000 mean 3.083333 -2.900000 std 3.493685 2.262742 min 0.750000 -4.500000 25% 1.075000 -3.700000 50% 1.400000 -2.900000 75% 4.250000 -2.100000 max 7.100000 -1.300000
text[ '票价' ] . describe( )
count 891.000000
mean 32.204208
std 49.693429
min 0.000000
25% 7.910400
50% 14.454200
75% 31.000000
max 512.329200
Name: 票价, dtype: float64
text[ '父母子女个数' ] . describe( )
count 891.000000
mean 0.381594
std 0.806057
min 0.000000
25% 0.000000
50% 0.000000
75% 0.000000
max 6.000000
Name: 父母子女个数, dtype: float64