pandas时间中提取年月日分钟小时和字符串替换及map等操作

生成一个DataFrame

import pandas as pd
from datetime import datetime

res = [{'name': 'asfds', 'age': 20}, {'name': 'asfds', 'age': 14}, {'name': 'asfds', 'age': 5},
       {'name': 'asfds', 'age': 86}, {'name': 'dfd', 'age': 34}, {'name': 'df', 'age': 45}]
df = pd.DataFrame(res)
df['tel'] = 1319745254
print(df)
"""
    name  age         tel
0  asfds   20  1319745254
1  asfds   14  1319745254
2  asfds    5  1319745254
3  asfds   86  1319745254
4    dfd   34  1319745254
5     df   45  1319745254
"""

字符串替换

# 字符串替换
# df['tel'] = df['tel'].astype(str).map(lambda x: x.replace(x[3:6], '***'))
df['tel'] = df['tel'].astype(str).str.slice_replace(3, 6, "***")
print(df)
"""
    name  age         tel
0  asfds   20  131***5254
1  asfds   14  131***5254
2  asfds    5  131***5254
3  asfds   86  131***5254
4    dfd   34  131***5254
5     df   45  131***5254
"""
"""
使用map字典 会进行默认替换操作
但是字典离得键要覆盖所有的字段中的唯一值  不然没有覆盖到的为NAN
"""
# map里面默认字典是替换里面所有值
df['name'] = df['name'].map({"asfds": "aaa", "dfd": "bbb", "df": "ccc"})
print(df)
"""
  name  age         tel
0  aaa   20  131***5254
1  aaa   14  131***5254
2  aaa    5  131***5254
3  aaa   86  131***5254
4  bbb   34  131***5254
5  ccc   45  131***5254
"""
# 只会替换这一个值其他的为Nan
df['name'] = df['name'].map({"asfds": "aaa"})
print(df)
"""
  name  age         tel
0  aaa   20  131***5254
1  aaa   14  131***5254
2  aaa    5  131***5254
3  aaa   86  131***5254
4  NaN   34  131***5254
5  NaN   45  131***5254
"""

合并两列

# 合并两列
df["姓名"] = list(map(lambda x, y: x + "_" + y, df["姓"], df["名"]))

时间提取和匹配

# 时间提取
df['ts'] = pd.to_datetime(df['ts'])
# 方法1
df["year"] = df["ts"].map(lambda x: x.year)
df["month"] = df["ts"].map(lambda x: x.month)
df["day"] = df["ts"].map(lambda x: x.day)
# 方法2
df['month'] = df['ts'].dt.month
df['day'] = df['ts'].dt.day
df['hour'] = df['ts'].dt.hour
df['minute'] = df['ts'].dt.minute
df = df[df['hour'].isin([1, 13])]
df = df[df['minute'].isin([30, 35])]

# 精准匹配
df = df[df['ts'] == datetime(2022, 11, 1, 0, 0)]
df = df[df['ts'] > datetime(2022, 11, 1, 0, 0)]

你可能感兴趣的:(pandas,python,pandas,python,数据分析)