欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python如何用columns參數(shù)獲取DataFrame各列的表頭名

 更新時(shí)間:2024年03月26日 10:37:11   作者:梧桐雪  
這篇文章主要介紹了python如何用columns參數(shù)獲取DataFrame各列的表頭名問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

用columns參數(shù)獲取DataFrame各列的表頭名

有時(shí)候,我們希望獲得一個(gè)已有數(shù)據(jù)表的各個(gè)表頭名(即列名),來看看這個(gè)表格中到底存儲(chǔ)了哪些數(shù)據(jù),我們就可以打印columns列表來實(shí)現(xiàn)這個(gè)功能。

我們可以用以下的代碼來查看:

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)方法很多,沒有找到最好的

如有二級(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ù)長半軸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 geemap的安裝步驟及環(huán)境配置

    python geemap的安裝步驟及環(huán)境配置

    geemap是基于GEE由吳秋生老師二次開發(fā)的一個(gè)包,geemap主要使用python來進(jìn)行實(shí)現(xiàn)相關(guān)功能,這篇文章主要介紹了geemap的詳細(xì)安裝步驟及環(huán)境配置,需要的朋友可以參考下
    2022-08-08
  • python基礎(chǔ)之多態(tài)

    python基礎(chǔ)之多態(tài)

    這篇文章主要介紹了python多態(tài),實(shí)例分析了Python中返回一個(gè)返回值與多個(gè)返回值的方法,需要的朋友可以參考下
    2021-10-10
  • Python接口自動(dòng)化之淺析requests模塊get請(qǐng)求

    Python接口自動(dòng)化之淺析requests模塊get請(qǐng)求

    這篇文章主要介紹了requests模塊get請(qǐng)求,在Python語言中,雖然提供了urllib2和urllib的庫,但是相比較而言,Requests仍然是實(shí)現(xiàn)接口測試最好的選擇,因?yàn)樗怯闷饋砀雍啽?/div> 2021-08-08
  • python3對(duì)拉勾數(shù)據(jù)進(jìn)行可視化分析的方法詳解

    python3對(duì)拉勾數(shù)據(jù)進(jìn)行可視化分析的方法詳解

    這篇文章主要給大家介紹了關(guān)于python3對(duì)拉勾數(shù)據(jù)進(jìn)行可視化分析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Python入門教程5. 字典基本操作【定義、運(yùn)算、常用函數(shù)】

    Python入門教程5. 字典基本操作【定義、運(yùn)算、常用函數(shù)】

    這篇文章主要介紹了Python字典基本操作,包括字典的基本定義、運(yùn)算與常用函數(shù)相關(guān)使用技巧,代碼注釋中備有詳盡說明,便于理解,需要的朋友可以參考下
    2018-11-11
  • Python多線程原理與用法實(shí)例剖析

    Python多線程原理與用法實(shí)例剖析

    這篇文章主要介紹了Python多線程原理與用法,結(jié)合具體的爬蟲實(shí)例剖析了多線程的相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-01-01
  • Python中防止sql注入的方法詳解

    Python中防止sql注入的方法詳解

    SQL注入是比較常見的網(wǎng)絡(luò)攻擊方式之一,它不是利用操作系統(tǒng)的BUG來實(shí)現(xiàn)攻擊,而是針對(duì)程序員編程時(shí)的疏忽,通過SQL語句,實(shí)現(xiàn)無帳號(hào)登錄,甚至篡改數(shù)據(jù)庫。下面這篇文章主要給大家介紹了關(guān)于Python中防止sql注入的方法,需要的朋友可以參考下。
    2017-02-02
  • Python中的int函數(shù)使用

    Python中的int函數(shù)使用

    這篇文章主要介紹了Python中的int函數(shù)使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python基于pandas爬取網(wǎng)頁表格數(shù)據(jù)

    Python基于pandas爬取網(wǎng)頁表格數(shù)據(jù)

    這篇文章主要介紹了Python基于pandas獲取網(wǎng)頁表格數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • python中time模塊指定格式時(shí)間字符串轉(zhuǎn)為時(shí)間戳

    python中time模塊指定格式時(shí)間字符串轉(zhuǎn)為時(shí)間戳

    本文主要介紹了python中time模塊指定格式時(shí)間字符串轉(zhuǎn)為時(shí)間戳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評(píng)論