前面有做了R的数据处理,想基于类似的需求框架,将pandas中数据处理的方法汇总,方便自己查看比对
import pandas as pd
txt_df = pd.read_csv(r'C:\Users\lstid\Desktop\测试\demo.txt',sep='\t')
# 只读取了第2 3列数据
xlsx_df = pd.read_excel(r'C:\Users\lstid\Desktop\测试\demo.xlsx',usecols=[1,2])
print(xlsx_df)
b c
0 s 10.0
1 qe 11.0
2 r 12.0
3 hy6 13.0
4 s 14.0
5 qe 15.0
# 跳过前2行数据不进行读取
df = pd.read_excel(r'C:\Users\lstid\Desktop\测试\demo.xlsx',header=None,skiprows= 2)
print(df)
0 1 2
0 2 qe 11.0
1 3 r 12.0
2 4 hy6 13.0
3 5 s 14.0
4 6 qe 15.0
5 7 r 16.0
f = open(r'C:\Users\lstid\Desktop\测试\demo.txt','r')
for i in f:
print(i)
print(type(i))
break
a b c
<class 'str'>
import pandas as pd
txt_df = pd.read_csv(r'C:\Users\lstid\Desktop\测试\demo.txt',sep='\t')
print(txt_df.head(3))
a b c
0 1 s 10.0
1 2 qe 11.0
2 3 r 12.0
print(txt_df.tail(4))
a b c
18 19 r 28.0
19 20 NaN NaN
20 21 NaN NaN
21 22 NaN NaN
import pandas as pd
txt_df = pd.read_csv(r'C:\Users\lstid\Desktop\测试\demo.txt',sep='\t')
# shape返回的是一个元组
print(txt_df.shape)
import pandas as pd
txt_df = pd.read_csv(r'C:\Users\lstid\Desktop\测试\demo.txt',sep='\t')
print(txt_df.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 22 entries, 0 to 21
Data columns (total 3 columns):
a 22 non-null int64
b 19 non-null object
c 19 non-null float64
dtypes: float64(1), int64(1), object(1)
memory usage: 608.0+ bytes
import pandas as pd
txt_df = pd.read_csv(r'C:\Users\lstid\Desktop\测试\demo.txt',sep='\t')
print(txt_df.describe())
a c
count 22.000000 19.000000
mean 11.500000 19.000000
std 6.493587 5.627314
min 1.000000 10.000000
25% 6.250000 14.500000
50% 11.500000 19.000000
75% 16.750000 23.500000
max 22.000000 28.000000
import pandas as pd
txt_df = pd.read_csv(r'C:\Users\lstid\Desktop\测试\demo.txt',sep='\t')
print(txt_df.columns.values)
['a' 'b' 'c']
import pandas as pd
txt_df = pd.read_csv(r'C:\Users\lstid\Desktop\测试\demo.txt',sep='\t')
print(txt_df.dtypes)
a int64
b object
c float64
dtype: object
# 导入数据集
import pandas as pd
mtcars = pd.read_excel(r'C:\Users\lstid\Desktop\测试\mtcars.xlsx')
print(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
0 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
1 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
4 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
5 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
6 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
7 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
8 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
9 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
10 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
11 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
12 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
13 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
14 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
15 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
16 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
17 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
18 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
19 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
20 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
21 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
22 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
23 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
24 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
25 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
26 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
27 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
28 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
29 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
30 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
31 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
# 筛选出cyl等于4的数据
df = mtcars[mtcars['cyl'] ==4]
print(df)
mpg cyl disp hp drat wt qsec vs am gear carb
2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
7 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
8 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
17 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
18 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
19 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
20 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
25 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
26 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
27 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
31 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
# 筛选cyl等于4并且vs等于0
df = mtcars[(mtcars['cyl'] ==4) & (mtcars['vs'] == 0)]
print(df)
mpg cyl disp hp drat wt qsec vs am gear carb
26 26.0 4 120.3 91 4.43 2.14 16.7 0 1 5 2
# 筛选cyl等于4或者mpg在21和22.8之中
df = mtcars[(mtcars['cyl'] ==4) | (mtcars['mpg'].isin([21,22.8]))]
print(df)
mpg cyl disp hp drat wt qsec vs am gear carb
0 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
1 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
7 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
8 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
17 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
18 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
19 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
20 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
25 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
26 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
27 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
31 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
# 选取第一行数据 返回还是数据框
df = mtcars.iloc[[0]]
print(df)
mpg cyl disp hp drat wt qsec vs am gear carb
0 21.0 6 160.0 110 3.9 2.62 16.46 0 1 4 4
# 选取前五行数据
df = mtcars.iloc[0:5]
print(df)
mpg cyl disp hp drat wt qsec vs am gear carb
0 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
1 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
4 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
df = mtcars[['mpg','cyl']].head()
print(df)
mpg cyl
0 21.0 6
1 21.0 6
2 22.8 4
3 21.4 6
4 18.7 8
vec = mtcars['vs']
print(vec.head())
0 0
1 0
2 1
3 1
4 0
Name: vs, dtype: int64
print(mtcars.columns.values)
df = mtcars.rename(columns={'mpg':'MPG'}) # 键是原名,值是修改后名称
print(df.columns.values)
['mpg' 'cyl' 'disp' 'hp' 'drat' 'wt' 'qsec' 'vs' 'am' 'gear' 'carb']
['MPG' 'cyl' 'disp' 'hp' 'drat' 'wt' 'qsec' 'vs' 'am' 'gear' 'carb']
print(mtcars.columns.values)
mtcars.columns = ['MPG', 'CYL', 'disp', 'hp', 'drat', 'wt' ,'qsec' ,'vs' ,'am' ,'gear' ,'carb']
print(mtcars.columns.values)
['mpg' 'cyl' 'disp' 'hp' 'drat' 'wt' 'qsec' 'vs' 'am' 'gear' 'carb']
['MPG' 'CYL' 'disp' 'hp' 'drat' 'wt' 'qsec' 'vs' 'am' 'gear' 'carb']
print(mtcars.head())
df = mtcars.sort_values(by= 'mpg',ascending = False) # False代表降序,反之为升序
print(df.head())
mpg cyl disp hp drat wt qsec vs am gear carb
0 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
1 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
4 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
mpg cyl disp hp drat wt qsec vs am gear carb
19 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
17 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
27 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
18 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
25 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
df = mtcars.sort_values(by= ['mpg','gear'],ascending = [False,True])
print(df.head())
mpg cyl disp hp drat wt qsec vs am gear carb
19 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
17 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
18 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
27 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
25 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
mtcars['new_cyl'] = mtcars['cyl'].apply(lambda x: 100 * x)
print(mtcars.head())
mpg cyl disp hp drat wt qsec vs am gear carb new_cyl
0 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 600
1 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 600
2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 400
3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 600
4 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 800
mtcars.reset_index(inplace=True)
# 另一种表述
df = mtcars.reset_index(inplace=False)
print(df)
index mpg cyl disp hp drat wt qsec vs am gear carb
0 0 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
1 1 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
2 2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
3 3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
4 4 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
subset对应的值是列名,表示只考虑这些列,将对应值相同的行进行去重。
默认值为subset=None表示考虑所有列。
keep='first'表示保留第一次出现的重复行,是默认值。keep另外两个取值为"last"和False,
分别表示保留最后一次出现的重复行和去除所有重复行。
inplace=True表示直接在原来的DataFrame上删除重复项,而默认值False表示生成一个副本。
df = mtcars.drop_duplicates(subset=['mpg','cyl'],keep='first',inplace=False)
print(df.head())
mpg cyl disp hp drat wt qsec vs am gear carb
0 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
4 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
5 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
mtcars['x'] = mtcars['mpg'].astype(str) + '_' + mtcars['cyl'].astype(str)
print(mtcars.head())
mpg cyl disp hp drat wt qsec vs am gear carb x
0 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 21.0_6
1 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 21.0_6
2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 22.8_4
3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 21.4_6
4 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 18.7_8
# 由于数据是浮点型先转换为字符型 如果本身就是字符串可不用先转换
mtcars['x'],mtcars['y'] = mtcars['wt'].astype(str).str.split('.', 1).str
print(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb x y
0 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 2 62
1 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 2 875
2 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 2 32
3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 3 215
4 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 3 44
表格的长宽变换再数据处理中也比较常见,尤其是在画图的时候
import pandas as pd
df = pd.DataFrame({
'id':[1,2,3],
'time':['a','b','c'],
'lr': [100,200,300],
'ht':[50,60,70]
})
print(df)
id time lr ht
0 1 a 100 50
1 2 b 200 60
2 3 c 300 70
df1 = df.melt(
id_vars= [ 'id','time'], # 要保留的主字段
var_name="标记", # 变换后的分类变量名称
value_name="值" # 变换后的值对应的列名称
)
print(df1)
id time 标记 值
0 1 a lr 100
1 2 b lr 200
2 3 c lr 300
3 1 a ht 50
4 2 b ht 60
5 3 c ht 70
df2 = df1.pivot_table(
index=['id','time'],
columns=["标记"],
values=["值"]
)
print(df2)
值
标记 ht lr
id time
1 a 50 100
2 b 60 200
3 c 70 300