pandas學(xué)習(xí)筆記

pandas 讀取 csv 文件

import pandas as pd
test_df = pd.read_csv("./test.csv")  # 將文件csv文件全部讀取
test_df = pd.read_csv(names=['a_from_lng','a_from_lat','b_from_lng','b_from_lat',],skiprows=10)  # 讀取這四列數(shù)據(jù),并跳過(guò)前面10行

查看pandas的列名以及列數(shù)

test_df.columns  # 列名
test_df.columns.size  # 列數(shù)
test_df.shape[1]  # 列數(shù)

pandas 查看某一列的全部數(shù)據(jù)或部分?jǐn)?shù)據(jù)

test_df['a_from_lng']  # 查看該列全部數(shù)據(jù)
test_df['a_from_lng'].head()  # 查看該列前面10條數(shù)據(jù)
test_df['a_from_lng'].head(100)  # 查看該列前面100條數(shù)據(jù)
test_df['a_from_lng'].tail()  # 查看該列最后10條數(shù)據(jù)
test_df['a_from_lng'].tail(100)  # 該看該列最后100條數(shù)據(jù)

查看所有列或者制定列的統(tǒng)計(jì)信息

test_df.describe()  # 所有列
test_df['a_from_lng'].describe()  # 指定列

利用前面幾列的信息生成新的列

def distance(x1, y1, x2, y2):
    return np.sqrt((x1-x2)**2+(y1-y2)**2)
test_df['start_distance_gap'] = map(lambda x1, y1, x2, y2: distance(x1, y1, x2, y2), \
    test_df['a_from_lng'], test_df['a_from_lat'], test_df['b_from_lng'], test_df['b_from_lat'])

pandas 處理時(shí)間特征

# 增加小時(shí)和分鐘特征
a_setup_time = pd.DatetimeIndex(test_df['a_setup_time'])
test_df["a_setup_hour"] = pd.Index(a_setup_time).hour
test_df["a_setup_minute"] = pd.Index(a_setup_time).minute

pandas 刪除無(wú)用特征,刪除列

test_df.drop('b_time', axis=1, inplace=True)

pandas 將csv文件保存到本地

test_df.to_csv('./feature.csv')
# 將特征文件保存,只保存第0,1,2,3列,并且不保存header(列名)
test_df.to_csv('./feature.csv', columns=[0, 1, 2, 3], header=False)  

pandas將一個(gè)list加入到df中

useful_order_count = [balabala...]
test_df["useful"] = pd.Series(useful_order_count)  # 在dataframe中增加新列useful,這個(gè)數(shù)據(jù)之前是一個(gè)list

DataFrame 轉(zhuǎn)換成 array 或 list

test_np = np.array(test_df)  # np.ndarray()
test_list = test_np.tolist()  # list

Series有多少不重復(fù)數(shù)據(jù),重復(fù)數(shù)據(jù)有多少

test_df['id'].value_counts()

輸出結(jié)果是每個(gè)值都對(duì)應(yīng)了多少數(shù)量

查看某一列有多少不重復(fù)的數(shù)據(jù)

tmp =  test_df.drop_duplicates(subset=['poi'])
print len(tmp)

查看DataFrame中有缺失值的列

na_count = test_df.isnull().sum().sort_values(ascending=False) # 找出test_df中有缺失值的列,并按照缺失的數(shù)量按照降序打印出來(lái)
na_count = na_count[na_count>0]  # 只查看缺失的
na_rate = na_count / len(test_df)
na_data = pd.concat([na_count,na_rate],axis=1,keys=['count','ratio']) 

pandas 獲得行名

test_df.index

pandas 顯示所有列的信息

pd.options.display.max_columns = None

將str轉(zhuǎn)換成時(shí)間

total_data_useful['Timestamp'] = pd.to_datetime(total_data_useful['Timestamp'])

dataframe 拼接

total_data = pd.concat([data1, data2, data3, data4, data5, data6])  # 按列拼接
total_data = pd.concat([data1, data2, data3, data4, data5, data6], axis=1) # 按行拼接

查看缺失數(shù)據(jù)

total= data_df.isnull().sum().sort_values(ascending=False)
percent = (data_df.isnull().sum()/data_df.isnull().count()).sort_values(ascending=False)
missing_data = pd.concat([total, percent], axis=1, keys=['Total','Percent'])
missing_data

對(duì)某一列進(jìn)行歸一化

col_name = 'Total_Flow'
tmp = (data_df[col_name] - data_df[col_name].mean()) / data_df[col_name].std()
data_df = data_df.drop([col_name], axis=1)
data_df[col_name] = tmp

設(shè)置精度

data.to_csv('out.csv',index = False, float_format = '%.4f')
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容