我有以下数据框:
import pandas as pd
import io
temp=u"""probe,genes,sample1
1415777_at,Pnliprp1 +OX(M6),20
1415777_at,Pllk +C6,20
1415884_at,Cela3b,47"""
df = pd.read_csv(io.StringIO(temp))
df
看起来像这样:
Out[23]:
probe genes sample1
0 1415777_at Pnliprp1 +OX(M6) 20
1 1415777_at Pllk +C6 20
2 1415884_at Cela3b 47
我想做的是在 gene
列中删除空格后的每个字符
在每一行。所以它看起来像:
probe genes sample1
0 1415777_at Pnliprp1 20
1 1415777_at Pllk 20
2 1415884_at Cela3b 47
如何实现?
最佳答案
我会使用向量化的 str
函数:
>>> df["genes"] = df["genes"].str.split().str[0]
>>> df
probe genes sample1
0 1415777_at Pnliprp1 20
1 1415777_at Pllk 20
2 1415884_at Cela3b 47
关于python - 删除 Pandas 列中每一行的空格后的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37177157/