android webservices 返回多行多列数据,Pandas: 如何将一列中的文本拆分为多行?
本文和大家分享的主要是使用
Pandas
將一列中的文本拆分為多行相關內容,一起來看看吧,希望對大家
學習
Pandas
有所幫助。
在數據處理過程中,經常會遇到以下類型的數據:
在同一列中,本該分別填入多行中的數據,被填在一行里了,然而在分析的時候,需要拆分成為多行。
在上圖中,列名為
”
Country
”,
index
為
4
和
5
的單元格內,值為”
UK/Australia
”和”
UK/Netherland
”。
下面
,我們來
扒一扒
將含有多值的內容分拆成多行的幾種方法。
加載數據
PS
:可以通過左右滑動來查看代碼
import pandas as pd
df = pd.DataFrame({'Country':['China','US','Japan','EU','UK/Australia','UK/Netherland'],
'Number':[100,150, 120, 90, 30, 2],
'Value':[1, 2, 3, 4, 5, 6],
'label':list('abcdef')})
df
Out[2]:
Country?Number ?Value label
0 ?????????China????100 ?????1 ????a
1 ????????????US????150 ?????2 ????b
2 ?????????Japan????120 ?????3 ????c
3 ????????????EU?????90 ?????4 ????d
4 ??UK/Australia ?????30?????5 ????e
5 ?UK/Netherland ??????2?????6 ????f
1 Method-1
分為如下幾步:
將含有多值的列進行拆分,然后通過
stack()
方法進行變換,并通過
index
的設置來完成
用
drop()
方法從
DataFrame
中刪除含有多值的列
然后用
join()
方法來合并
df.drop('Country', axis=1).join(df['Country'].str.split('/',expand=True).stack().reset_index(level=1, drop=True).rename('Country'))
Out[3]:
Number ?Value label ????Country
0 ????100 ?????1????a ??????China
1 ????150 ?????2????b ?????????US
2 ????120 ?????3????c ??????Japan
3 ?????90 ?????4????d ?????????EU
4 ?????30 ?????5????e ?????????UK
4 ?????30 ?????5????e ??Australia
5 ??????2 ?????6????f ?????????UK
5 ??????2 ?????6????f ?Netherland
過程分步介紹
df['Country'].str.split('/', expand=True).stack()
Out[4]:
0 ?0 ????????China
1 ?0 ???????????US
2 ?0 ????????Japan
3 ?0 ???????????EU
4 ?0 ???????????UK
1 ????Australia
5 ?0 ???????????UK
1 ???Netherland
dtype: object
df['Country'].str.split('/', expand=True).stack().reset_index(level=1,drop=True)
Out[5]:
0 ????????China
1 ???????????US
2 ????????Japan
3 ???????????EU
4 ???????????UK
4 ????Australia
5 ???????????UK
5 ???Netherland
dtype: object
df['Country'].str.split('/', expand=True).stack().reset_index(level=1,drop=True).rename('Country')
Out[6]:
0 ????????China
1 ???????????US
2 ????????Japan
3 ???????????EU
4 ???????????UK
4 ????Australia
5 ???????????UK
5 ???Netherland
Name: Country, dtype: object
df.drop('Country', axis=1)
Out[7]:
Number ?Value label
0 ????100 ?????1????a
1 ????150 ?????2????b
2 ????120 ?????3????c
3 ?????90 ?????4????d
4 ?????30 ?????5????e
5 ??????2 ?????6????f
2 Method-2
該方法的思路跟
Method-1
基本是一樣的,只是在具體的細節方面有些差異。代碼如下:
df['Country'].str.split('/', expand=True).stack().reset_index(level=0).set_index('level_0').rename(columns={0:'Country'}).join(df.drop('Country',axis=1))
Out[8]:
Country?Number ?Value label
0 ??????China????100 ?????1 ????a
1 ?????????US????150 ?????2 ????b
2 ??????Japan????120 ?????3 ????c
3 ?????????EU?????90 ?????4 ????d
4 ?????????UK?????30 ?????5 ????e
4 ??Australia ?????30?????5 ????e
5 ?????????UK??????2 ?????6 ????f
5 ?Netherland ??????2?????6 ????f
過程分步介紹如下:
df['Country'].str.split('/', expand=True).stack().reset_index(level=0)
Out[9]:
level_0 ??????????0
0 ???????0??????China
0 ???????1?????????US
0 ???????2??????Japan
0 ???????3?????????EU
0 ???????4?????????UK
1 ???????4??Australia
0 ???????5?????????UK
1 ???????5?Netherland
df['Country'].str.split('/', expand=True).stack().reset_index(level=0).set_index('level_0')
Out[10]:
level_0
0 ????????????China
1 ???????????????US
2 ????????????Japan
3 ???????????????EU
4 ???????????????UK
4 ????????Australia
5 ???????????????UK
5 ???????Netherland
df['Country'].str.split('/', expand=True).stack().reset_index(level=0).set_index('level_0').rename(columns={0:'Country'})
Out[11]:
Country
level_0
0 ????????????China
1 ???????????????US
2 ????????????Japan
3 ???????????????EU
4 ???????????????UK
4 ????????Australia
5 ???????????????UK
5 ???????Netherland
df.drop('Country', axis=1)
Out[12]:
Number ?Value label
0 ????100 ?????1????a
1 ????150 ?????2????b
2 ????120 ?????3????c
3 ?????90 ?????4????d
4 ?????30 ?????5????e
5 ??????2 ?????6????f
3
閑談
當然,將某列中含有多值的單元拆分成多行,還有其他方法,各位小伙伴們可以研究下
~~
來源:博客園
贊 | 0
總結
以上是生活随笔為你收集整理的android webservices 返回多行多列数据,Pandas: 如何将一列中的文本拆分为多行?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 图标的格式,Androi
- 下一篇: html中after伪类原理,css伪类