DataFrame 中某一列是元組形式拆成兩列

2021-09-18 06:49:44 字數 1513 閱讀 3762

1.有元組的拆分

df3_new.head()
migrate

number1

migrate

0-11441.0

(北京, 廊坊)

1-9745.0

(深圳, 惠州)

2-7393.0

(上海, 蘇州)

3-7053.0

(東莞, 惠州)

4-4451.0

(北京, 保定)

想把這個裡面的migrate拆成兩列

#將元組拆包

#dataframe 中某一列是元組形式拆成兩列

for i in range (len(df3_new)):

l1 = [list(j)[0] for j in df3_new['migrate']]

l2 = [list(k)[1] for k in df3_new['migrate']]

l1l2

df3_new['地區'] = l1

df3_new['city'] = l2

df3_new

df3_new1 = df3_new.drop('migrate', axis = 1)

df3_new.head()

number1

地區city

-11441.0

北京廊坊

-9745.0

深圳惠州

-7393.0

上海蘇州

-7053.0

東莞惠州

-4451.0

北京保定

df3_new

df3_new['地區'] = df3_new['migrate'].map(lambda x :x.split(',')[0])

df3_new['city'] = df3_new['migrate'].map(lambda x :x.split(',')[1])

df3_new

3.如何遍歷一列陣列並重新對陣列進行編號賦值,形如['a', 'b', 'c', 'c', 'd']變成[1,2,3,3,4]

思路:#1.將列表用set進行去重

#2.取長度,並生成等長的列表

#3將1,2的結果構建字典

#4對原始的列表進行字典對映

上**:

city1 = set(list(city)) 

list(city1)[:10]

['遂寧', '北京', '樂山', '菏澤', '畢節', '棗莊', '運城', '呂梁', '吐魯番', '張家界']
n = [ i for i in range(len(city1))]

c = list(city1)

data= dict(map(lambda x,y:[x,y], c,n))#將節點和標籤建立字典

plane['n1'] = plane['地區'].map(data)#map函式將 節點和編號對應

DataFrame對某一列中單元格的操作 map

會遇到dataframe中單元格既不是nan 又不是none 的 比如這種 created at catefirst 2020 04 01 取得這種catefirst為空的需要用到 pd pd catefirst 這時候能把空值找出 如果是對這一列來操作的話 處理空值 填充為0 或者別的 可以用ma...

dataframe多列合併成一列

dataframe的幾列資料合併成為一列 dataframe的幾列資料合併成為一列 1.1 方法歸納 1.2 str.cat函式詳解 1.2.1 語法格式 1.2.2 引數說明 1.2.3 核心功能 1.2.4 常見範例 dataframe的幾列資料合併成為一列 1.1 方法歸納 使用 直接將多列合...

C DataTable 某一列求和

c datatable 某一列求和 1.列為數字型別double total convert.todouble datatable.compute sum 需要求和的引數 2.列為string 型別 先轉為數字型別 再求和 遇到是採用了這個方法 會報錯,加using system.linq 命名空間...