[英]Split Columns in pandas with str.split and keep values
So I am stuck with a problem here: 所以我在这里遇到了一个问题:
I have a pandas dataframe which looks like the following: 我有一个熊猫数据框,如下所示:
ID Name Value
0 Peter 21,2
1 Frank 24
2 Tom 23,21/23,60
3 Ismael 21,2/ 21,54
4 Joe 23,1
and so on...
What I am trying to is to split the "Value" column by the slash forward (/) but keep all the values, which do not have this kind of pattern. 我要尝试的是用斜杠(/)分隔“值”列,但保留所有不具有这种模式的值。
Like here: 像这儿:
ID Name Value
0 Peter 21,2
1 Frank 24
2 Tom 23,21
3 Ismael 21,2
4 Joe 23,1
How can I achieve this? 我该如何实现? I tried the str.split method but it's not giving me the solution I want.
我尝试了str.split方法,但没有给我想要的解决方案。 Instead, it returns NaN as can be seen in the following.
而是返回NaN,如下所示。
My Code: df['Value']=df['value'].str.split('/', expand=True)[0]
Returns:
ID Name Value
0 Peter NaN
1 Frank NaN
2 Tom 23,21
3 Ismael 21,2
4 Joe Nan
All I need is the very first Value before the '/' is coming. 我需要的只是在'/'出现之前的第一个值。
Appreciate any kind of help! 感谢任何帮助!
Remove expand=True
for return lists and add str[0]
for select first value: 删除
expand=True
返回列表,并添加str[0]
选择第一个值:
df['Value'] = df['Value'].str.split('/').str[0]
print (df)
ID Name Value
0 0 Peter 21,2
1 1 Frank 24
2 2 Tom 23,21
3 3 Ismael 21,2
4 4 Joe 23,1
If performance is important use list comprehension: 如果性能很重要,请使用清单理解:
df['Value'] = [x.split('/')[0] for x in df['Value']]
pandas.Series.str.replace
with regex pandas.Series.str.replace
df.assign(Value=df.Value.str.replace('/.*', ''))
ID Name Value
0 0 Peter 21,2
1 1 Frank 24
2 2 Tom 23,21
3 3 Ismael 21,2
4 4 Joe 23,1
Optionally, you can assign results directly back to dataframe (可选)您可以将结果直接分配回数据框
df['Value'] = df.Value.str.replace('/.*', '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.