python如何用columns參數(shù)獲取DataFrame各列的表頭名
用columns參數(shù)獲取DataFrame各列的表頭名
有時(shí)候,我們希望獲得一個(gè)已有數(shù)據(jù)表的各個(gè)表頭名(即列名),來(lái)看看這個(gè)表格中到底存儲(chǔ)了哪些數(shù)據(jù),我們就可以打印columns列表來(lái)實(shí)現(xiàn)這個(gè)功能。
我們可以用以下的代碼來(lái)查看:
import pandas as pd
dict_data = {
'student':["Li Lei","Han Meimei","Tom"],
'score' :[95,98,92],
'gender':['M','F','M']
}
df_data1 = pd.DataFrame(dict_data)
df_data2 = pd.DataFrame(dict_data,columns=['gender','student','score'])
print(df_data1)
print(df_data2)
print(df_data1.columns)
print(df_data2.columns)
運(yùn)行之后的結(jié)果如下所示:
student score gender
0 Li Lei 95 M
1 Han Meimei 98 F
2 Tom 92 M
gender student score
0 M Li Lei 95
1 F Han Meimei 98
2 M Tom 92
Index(['student', 'score', 'gender'], dtype='object')
Index(['gender', 'student', 'score'], dtype='object')
[Finished in 4.2s]
課件,columns列表本質(zhì)上是一個(gè)Index類型的object。
pandas dataframe的一些技巧
1. 按日期排序
df211['rq']=pd.to_datetime(df211.rq) df211=df211.sort_values(['rq']).reset_index(drop=True)
1) df = df.sort_values(by='date') 應(yīng)該也行
2) 以上操作后'rq'會(huì)變成timestamp類型,轉(zhuǎn)換為datetime類型:
ts=(list(df211['rq'])[0]).date() ts=(list(df211['rq'])[0]).to_pydatetime()
此外,獲取某一列的日期范圍并排序:
def change_date(s):
s = datetime.datetime.strptime(s, "%Y-%m-%d") # 把日期標(biāo)準(zhǔn)化,轉(zhuǎn)化結(jié)果如:2015/1/4 => 2015-01-04 00:00:00
s = str(s) # 上一步把date轉(zhuǎn)化為了時(shí)間格式,因此要把date轉(zhuǎn)回str格式
return s[:10] # 只獲取年月日,即“位置10”之前的字符串
data = list(df_0328['rq'].unique())
data=list(map(change_date,data) )
print(type(data))
data.sort(key=lambda date: datetime.datetime.strptime(date, "%Y-%m-%d"))2.去掉特定值行列
df211 = df21.drop(df21[df21['road_name']!='匯新家園'].index).reset_index(drop=True)
3.統(tǒng)計(jì)列中各種值出現(xiàn)次數(shù)
df2['road_name'].value_counts()
4.處理一張表內(nèi)嵌的多張表&處理多級(jí)表頭
1)
xl = pd.ExcelFile('路區(qū)模型.xlsx',engine='openpyxl')
sheet_names = xl.sheet_names # 所有的sheet名稱
print(sheet_names)2)方法很多,沒(méi)有找到最好的
如有二級(jí)表頭,則:
df0512 = pd.read_excel('路區(qū)模型.xlsx',engine='openpyxl',\
sheet_name='表2',header=[0,1])5.取出某一列中的數(shù)值/去掉非數(shù)值項(xiàng)
使用pd.to_numeric
b_=[x for x in (list(df21[('SF', 'B端攬收單量')])) if not np.isnan(pd.to_numeric(x, errors='coerce'))]6.去某一列字符型前十個(gè)字符
df_deliver['date'] = df_deliver['create_time'].str[:10]
7.去除日期中小時(shí)
df_deliver['hour'] = pd.to_datetime(df_deliver['time']).apply(lambda x:x.hour)
8.坐標(biāo)轉(zhuǎn)換
def GCJ2WGS(lat,lon):
# location格式如下:locations[1] = "113.923745,22.530824"
a = 6378245.0 # 克拉索夫斯基橢球參數(shù)長(zhǎng)半軸a
ee = 0.00669342162296594323 #克拉索夫斯基橢球參數(shù)第一偏心率平方
PI = 3.14159265358979324 # 圓周率
# 以下為轉(zhuǎn)換公式
x = lon - 105.0
y = lat - 35.0
dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * np.sqrt(abs(x));
dLon += (20.0 * np.sin(6.0 * x * PI) + 20.0 * np.sin(2.0 * x * PI)) * 2.0 / 3.0;
dLon += (20.0 * np.sin(x * PI) + 40.0 * np.sin(x / 3.0 * PI)) * 2.0 / 3.0;
dLon += (150.0 * np.sin(x / 12.0 * PI) + 300.0 * np.sin(x / 30.0 * PI)) * 2.0 / 3.0;
#緯度
dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * np.sqrt(abs(x));
dLat += (20.0 * np.sin(6.0 * x * PI) + 20.0 * np.sin(2.0 * x * PI)) * 2.0 / 3.0;
dLat += (20.0 * np.sin(y * PI) + 40.0 * np.sin(y / 3.0 * PI)) * 2.0 / 3.0;
dLat += (160.0 * np.sin(y / 12.0 * PI) + 320 * np.sin(y * PI / 30.0)) * 2.0 / 3.0;
radLat = lat / 180.0 * PI
magic = np.sin(radLat)
magic = 1 - ee * magic * magic
sqrtMagic = np.sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * np.cos(radLat) * PI);
wgsLon = lon - dLon
wgsLat = lat - dLat
return wgsLat,wgsLon
lat = list(df_1['lat'])
lon=list(df_1['lon'])
data=list(map(GCJ2WGS,lat,lon) )總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python任務(wù)調(diào)度利器之APScheduler詳解
所謂的任務(wù)調(diào)度是指安排任務(wù)的執(zhí)行計(jì)劃,即何時(shí)執(zhí)行,怎么執(zhí)行等。這篇文章主要介紹了Python任務(wù)調(diào)度利器之APScheduler詳解,需要的朋友可以參考下2020-04-04
python實(shí)現(xiàn)冒泡排序算法的兩種方法
本篇文章主要介紹了python實(shí)現(xiàn)冒泡排序的兩種方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
解決安裝pytorch因網(wǎng)速問(wèn)題失敗的情況
這篇文章主要介紹了解決安裝pytorch因網(wǎng)速問(wèn)題失敗的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python調(diào)用pyaudio使用麥克風(fēng)錄制wav聲音文件的教程
這篇文章主要介紹了python調(diào)用pyaudio使用麥克風(fēng)錄制wav聲音文件的教程,詳細(xì)的給大家介紹了pyaudio庫(kù)的安裝與使用,需要的朋友可以參考下2019-06-06
ansible-playbook實(shí)現(xiàn)自動(dòng)部署KVM及安裝python3的詳細(xì)教程
這篇文章主要介紹了ansible-playbook實(shí)現(xiàn)自動(dòng)部署KVM及安裝python3的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Python+matplotlib實(shí)現(xiàn)堆疊圖的繪制
Matplotlib作為Python的2D繪圖庫(kù),它以各種硬拷貝格式和跨平臺(tái)的交互式環(huán)境生成出版質(zhì)量級(jí)別的圖形。本文將利用Matplotlib庫(kù)繪制堆疊圖,感興趣的可以了解一下2022-03-03

