我有一个数据框,我想从中提取数字,如果'transfer'字是 在8列上,它应该从位置13、15字符中提取出来,否则 应该从第21位15个字符中提取
SELECT IFNULL(SUM(ri.price),0) AS total, m.month
FROM (
SELECT 'Jan' AS MONTH
UNION SELECT 'Feb' AS MONTH
UNION SELECT 'Mar' AS MONTH
UNION SELECT 'Apr' AS MONTH
UNION SELECT 'May' AS MONTH
UNION SELECT 'Jun' AS MONTH
UNION SELECT 'Jul' AS MONTH
UNION SELECT 'Aug' AS MONTH
UNION SELECT 'Sep' AS MONTH
UNION SELECT 'Oct' AS MONTH
UNION SELECT 'Nov' AS MONTH
UNION SELECT 'Dec' AS MONTH
) AS m
LEFT JOIN table_u pu
ON MONTH(STR_TO_DATE(CONCAT(pu.created_date, '2019'),'%M %Y')) = MONTH(pu.created_date)
AND YEAR(pu.created_date) = '2019'
LEFT JOIN table ri
ON ri.id = pu.id
GROUP BY m.month
ORDER by 1+1;
答案 0 :(得分:0)
尝试一下:
import pandas as pd
import numpy as np
df['new_extract_column'] = np.nan
df.loc[ df['column8'].str.contains('transfer'), 'new_extract_column' ] = df[ df['column8'].str.contains('transfer') ].apply(lambda x: x[13:16])
df.loc[ ~df['column8'].str.contains('transfer'), 'new_extract_column' ] = df[ df['column8'].str.contains('transfer') ].apply(lambda x: x[15: 22])
答案 1 :(得分:0)
如果您的数据后有空格,则可以使用分割字符串方法。否则您可以使用正则表达式
df = pd.DataFrame([['On-Line Transfer - 01901091900014'], ['On-Line Transfer - 02501091900004'], ['On-Line Transfer - 03601091900018'], ['Transfer - 03631081900095'], ['Transfer - 03829081900083']], columns=['Particular'])
df["Particular"].str.split().str[-1]