contains方法_【原创】Pandas数据处理系列(二):常用处理方法笔记
Pandas的魅力在于處理數據的靈活性,但是由于太靈活,會導致使用者很容易忘記各類方法。在Pandas學習這件事情上,真正體現了好記性不如爛筆頭的方法特性。故特用此文章記錄Pandas常用的數據處理方法,需要用的時候,打開此文章直接查閱即可。
1.多列排序
enddf=newdf.sort_values(['date','Buypower'],ascending=[True,False])
2.數組差集
set(tradedate).difference(set(dailylist))
3.按列條件更新 (等同SQL: update xxx=zzz where yyy)
datadf.loc[(datadf.bigamount > 300 * 10000) & (datadf.bigamount <= 1000 * 10000), 'bigamount'] = 300 * 10000
4.列轉換成date類型
subdf2.date=pd.to_datetime(subdf2.date)
5.設置多級索引:
df.set_index(['ts_code','date'])
6.相鄰行操作:
df=df.apply(lambda x:x-x.shift(1))
7.lambda用if:
df=df.apply(lambda x:x/x.shift(1) if x.name[0] == x.shift(1).name[0] else x )
8.刪除空行:
weekdf=weekdf.dropna()
9.matplotlib畫柱狀圖:
subdf2.plot(kind='bar',title=code)
10.matplotlib畫水平線:
plt.axhline(y=8000, color='r', linestyle='-')
11.matplotlib柱狀圖上添加數量標簽文字:
for x, y in enumerate(subdf2['vol'].values):
print(x, y)
plt.text(x-0.5, y+50, "%s" %y)
12.列重命名:
enddf=enddf.rename(columns={'index':'ts_code'})
13.列截取字符串:
mergdf.list_date = mergdf.list_date.str[:4]
14.timedelta取出天數:
mergdf.list_date=mergdf.list_date.astype('timedelta64[D]').astype(int)
15.Series轉換成dataframe:
grpdf=grpdf.to_frame()
16.某列包含字符串,相當于SQL中的 like %xx%。
df0[df0.UpReason.str.contains('xxx')
17.刪除某列
df.drop('Type', axis='columns')
總結
以上是生活随笔為你收集整理的contains方法_【原创】Pandas数据处理系列(二):常用处理方法笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 获取今天数据,ThinkPHP
- 下一篇: udp 使用connect优点_一文搞懂