必看 python數(shù)據(jù)分析之pandas數(shù)據(jù)選染啥辍:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
刪除某一列
del ans['dis']
下一行減上一行(x是colums名)
s1 = ans.x.diff()
對DataFrame每一列的每個(gè)數(shù)的處理
x1=[12,435,23]
x2=[21,42,452]
x3 = map(lambda x,y:x-y,x1,x2)
print x3
x3_1 = map(lambda x:x**2,x3)
print x3_1
x3_sum = sum(x3_1)
print x3_sum
計(jì)算航向角與航跡角的偏差,對DataFrame的列進(jìn)行計(jì)算處理
import pandas as pd
import math
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
ans=pd.read_table(r'/home/wanghan/AMAP_intern/dataAnalysis/Yaw and Flight Path comparision/trajectory5961-5985.txt',sep=',')
ans=ans[(ans['time_stamp'] > 5961) & (ans['time_stamp'] < 5985) & (ans['index'] % 60 == 1)]
ans.reset_index().to_csv(r'./Yaw and Flight Path comparision/down_sample_trajectory5961-5985.txt',index=False)
yaw_angle = ans['yaw(deg)']
yaw_angle = yaw_angle.values
s1 = ans.x.diff() //得到的是series類型
s2 = ans.y.diff()
s3 = s1/s2
a1= s3.values
a2 = map(lambda x:math.atan(x),a1)
a3 = map(lambda x:math.degrees(x)+180, a2)
bias = yaw_angle - a3
bias_ = pd.DataFrame(bias)
bias_.columns = ['angle_bias']
plt.figure()
plt.title("angle_bias")
# plt.xlabel("time")
plt.ylabel("degree")
plt.plot(bias_.index,bias_['angle_bias'])
plt.show()
plt.savefig('3.png')
給yaw這一列都加上一個(gè)數(shù)
df1=pd.read_table(r'D:\Kevin\Alibaba_summer\Amap_Intern\test1.txt',sep=',')
df1.loc[:,'yaw(deg)'] = df1.loc[:,'yaw(deg)'] + 0.333
df1.to_csv(r'D:\Kevin\Alibaba_summer\Amap_Intern\test2.txt', sep=',',index=False)
根據(jù)時(shí)間這一列同波,把其他列按照時(shí)間相減 .set_index('根據(jù)的列名')
df1 = pd.read_table(r'D:\Ubuntu_disk\test1.txt', sep = ',')
df2 = pd.read_table(r'D:\Ubuntu_disk\test2.txt', sep = ',')
ans = df1.set_index('time_stamp') - df2.set_index('time_stamp')
ans = ans.reset_index()
多張圖畫在同一張圖上
python Matplotlib 可視化總結(jié)歸納(二) 繪制多個(gè)圖像單獨(dú)顯示&多個(gè)函數(shù)繪制于一張圖
對某一列的某一個(gè)值按照條件進(jìn)行處理
#常規(guī)方式
import pandas as pd
df = pd.DataFrame({'one':['a', 'a', 'b', 'c'], 'two':[3,1,2,3], 'three':['C','B','C','A']})
print(df)
df.loc[df['two']==2, 'one']='x' #修改列"one"的值聪建,推薦使用.loc
print(df)
df.one[df.two==2]='x'
print(df)
DataFrame選取偶數(shù)行和奇數(shù)行
#%%
import pandas as pd
import numpy as np
#%%
np.random.seed(1071)
df = pd.DataFrame(
np.random.randint(1, 30, (7, 2)), columns=list('AB'),
index=range(1, 8)
)
#
# A B
#1 28 22
#2 17 13
#3 10 3
#4 19 25
#5 2 6
#6 22 27
#7 28 26
df.iloc[::2] # 奇數(shù)行
# A B
#1 28 22
#3 10 3
#5 2 6
#7 28 26
df.iloc[1::2] # 偶數(shù)行
# A B
#2 17 13
#4 19 25
#6 22 27