【问题标题】:Splitting the data of one excel column into two columns sing python将一个excel列的数据拆分为两列sing python
【发布时间】:2023-02-09 03:34:44
【问题描述】:

我有将一个包含数字和字母的 excel 列的内容拆分为两列的问题,一列中的数字和另一列中的字母。

正如您在第一张照片中看到的那样,数字和字母之间没有空格,但好消息是字母始终为“ms”。我需要一种方法将它们拆分为第二张照片。

我尝试使用替换,但没有用。它没有分裂他们。

有没有其他方法。

【问题讨论】:

  • pd.Series.str.split('(ms)$', expand=True)
  • 列的名称到底在哪里?
  • 对不起,但它没有用

标签: excel pandas dataframe split


【解决方案1】:

您可以使用extract 方法。这是一个例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    相关资源
    最近更新 更多