英文:
Splitting the data of one excel column into two columns sing python
问题
我有一个问题,需要将包含数字和字母的一个Excel列的内容拆分为两列,一个列中包含数字,另一个列中包含字母。
如您在第一张照片中所见,数字和字母之间没有空格,但好的一点是字母始终是"ms"。我需要一种方法将它们拆分成第二张照片中的样子。
尝试使用替换方法,但它没有起作用,没有将它们拆分开。
是否有其他方法?
英文:
I have problem of splitting the content of one excel column which contains numbers and letters into two columns the numbers in one column and the letters in the other.
As can you see in the first photo there is no space between the numbers and the letters, but the good thing is the letters are always "ms". I need a method split them as in the second photo.
Before
After
I tried to use the replace but it did not work. it did not split them.
Is there any other method.
答案1
得分: 1
你可以使用extract
方法。以下是一个示例:
df = pd.DataFrame({'time': ['34ms', '239ms', '126ms']})
df[['time', 'unit']] = df['time'].str.extract('(\d+)(\D+)')
# 将时间列转换为整数
df['time'] = df['time'].astype(int)
print(df)
# 输出:
# time unit
# 0 343 ms
# 1 239 ms
# 2 126 ms
英文:
You can use the extract
method. Here is an example:
df = pd.DataFrame({'time': ['34ms', '239ms', '126ms']})
df[['time', 'unit']] = df['time'].str.extract('(\d+)(\D+)')
# convert time column into integer
df['time'] = df['time'].astype(int)
print(df)
# output:
# time unit
# 0 343 ms
# 1 239 ms
# 2 126 ms
答案2
得分: 0
以下是翻译好的内容:
这很简单。 <br>
你需要使用pandas.Series.str.split <br>
这里附上语法:pandas.Series.str.split
代码应该如下:
import pandas as pd
data_before = {'data': ['34ms', '56ms', '2435ms']}
df = pd.DataFrame(data_before)
result = df['data'].str.split(pat='(\d+)', expand=True)
result = result.loc[:, [1, 2]]
result.rename(columns={1: 'number', 2: 'string'}, inplace=True)
输出:
print(result)
英文:
It is pretty simple. <br>
You need to use pandas.Series.str.split <br>
Attaching the Syntax here :- pandas.Series.str.split
The Code should be
import pandas as pd
data_before = {'data' : ['34ms','56ms','2435ms']}
df = pd.DataFrame(data_before)
result = df['data'].str.split(pat='(\d+)',expand=True)
result = result.loc[:,[1,2]]
result.rename(columns={1:'number', 2:'string'}, inplace=True)
Output : -
print(result)
答案3
得分: 0
用Notepad++打开包含此数据的Excel文件,将ms
更改为,ms
,另存为CSV(逗号分隔)
。然后在Excel中打开。
英文:
Open the Excel file with this data in Notepad++, change ms
for ,ms
, save as CSV (comma delimited)
. Open with Excel.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论