I am trying to split on column named as "variable" into two another column "Type" and "Parameter"
我正在尝试将名为"variable"的列拆分为另外两列"Type"和"Parameter"
BatchNumber PhaseNumber SiteID variable Values
0 4552694035 0020B 2 min_tempC 27.0
1 4552694035 OverAll 2 max_tempF 24.0
I tried to use below code
我试着使用下面的代码
weatherData = weatherData['variable'].str.split('_', 1)
But not getting expected result. The expected result is as below.
但是没有得到预期的结果。预期的结果如下。
BatchNumber PhaseNumber SiteID variable Values Type Parameter
0 4552694035 0020B 2 min_tempC 27.0 min tempC
1 4552694035 OverAll 2 max_tempF 24.0 max tempF
Any body knows.. how to get it?
任何身体知道. .如何得到它?
3 个解决方案
#1
2
Use DataFrame.pop
for extract column with split
and parameter expand=True
for DataFrame
:
使用DataFrame。带分割和参数展开的提取列的pop = DataFrame的True:
weatherData[['Type','Parameter']]=weatherData.pop('variable').str.split('_', 1, expand=True)
print (weatherData)
BatchNumber PhaseNumber SiteID Values Type Parameter
0 4552694035 0020B 2 27.0 min tempC
1 4552694035 OverAll 2 24.0 max tempF
If want also original column remove pop
:
如果也要原列删除pop:
weatherData[['Type','Parameter']] = weatherData['variable'].str.split('_', 1, expand=True)
print (weatherData)
BatchNumber PhaseNumber SiteID variable Values Type Parameter
0 4552694035 0020B 2 min_tempC 27.0 min tempC
1 4552694035 OverAll 2 max_tempF 24.0 max tempF
#2
0
This is possible via pd.Series.str.split
:
这可以通过ps . series.str.split:
df['Type'], df['Parameter'] = df['variable'].str.split('_')
# BatchNumber PhaseNumber SiteID variable Values Type Parameter
# 0 4552694035 0020B 2 min_tempC 27.0 min max
# 1 4552694035 OverAll 2 max_tempF 24.0 tempC tempF
#3
0
Using assign
, zip
, and dict
unpacking
使用分配、压缩和命令解包
df.assign(**dict(zip(('Type', 'Parameter'), zip(*df.variable.str.split('_')))))
BatchNumber PhaseNumber SiteID variable Values Type Parameter
0 4552694035 0020B 2 min_tempC 27.0 min tempC
1 4552694035 OverAll 2 max_tempF 24.0 max tempF
#1
2
Use DataFrame.pop
for extract column with split
and parameter expand=True
for DataFrame
:
使用DataFrame。带分割和参数展开的提取列的pop = DataFrame的True:
weatherData[['Type','Parameter']]=weatherData.pop('variable').str.split('_', 1, expand=True)
print (weatherData)
BatchNumber PhaseNumber SiteID Values Type Parameter
0 4552694035 0020B 2 27.0 min tempC
1 4552694035 OverAll 2 24.0 max tempF
If want also original column remove pop
:
如果也要原列删除pop:
weatherData[['Type','Parameter']] = weatherData['variable'].str.split('_', 1, expand=True)
print (weatherData)
BatchNumber PhaseNumber SiteID variable Values Type Parameter
0 4552694035 0020B 2 min_tempC 27.0 min tempC
1 4552694035 OverAll 2 max_tempF 24.0 max tempF
#2
0
This is possible via pd.Series.str.split
:
这可以通过ps . series.str.split:
df['Type'], df['Parameter'] = df['variable'].str.split('_')
# BatchNumber PhaseNumber SiteID variable Values Type Parameter
# 0 4552694035 0020B 2 min_tempC 27.0 min max
# 1 4552694035 OverAll 2 max_tempF 24.0 tempC tempF
#3
0
Using assign
, zip
, and dict
unpacking
使用分配、压缩和命令解包
df.assign(**dict(zip(('Type', 'Parameter'), zip(*df.variable.str.split('_')))))
BatchNumber PhaseNumber SiteID variable Values Type Parameter
0 4552694035 0020B 2 min_tempC 27.0 min tempC
1 4552694035 OverAll 2 max_tempF 24.0 max tempF