python解决数据框中添加一行或者一列(DataFrame的行列处理)

1、解决数据框中添加一行(给定值)

###原数据框
 data_Peak_2 = pd.DataFrame({"Peak_density": np.ndarray.tolist(hmax_x), "Peak_H": hmax_y, "DP_DPV": pd_pv, "DP_DPV_2": pd_pv2})
            
###实现在数据框中添加一行
event = {}
event["Peak_density"] =(df.iloc[0, 2]+df.iloc[1, 2])/2 df.iloc[0, 2]#(df.iloc[0, 2]+df.iloc[1, 2])/2
event["Peak_H"] = 0.001
event["DP_DPV"] = 0.02
event["DP_DPV_2"] = 0.02
data_Peak_2 = data_Peak_2.append(event, ignore_index=true)
           

2、解决数据框中添加一列

import laspy
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter
import os, glob
from scipy.signal import savgol_filter

path=r"E:\***\20210809数据统计\***" ###原数据框
pathDir = os.listdir(path)
for f in pathDir:
    df = pd.read_csv(os.path.join(path, f))
    print("df1",df)
    ###求曲线
    y = df.cover
    x = df.H
    z1 = np.polyfit(x, y, 10)  # 用10次多项式拟合
    p1 = np.poly1d(z1)
    yvals = np.polyval(z1, x)  # 也可以使用yvals=np.polyval(z1,x)
    yvals2 = savgol_filter(yvals, 5, 2, mode='nearest')
    ###曲线结束
    D_value2=yvals2 - 0.005
    df["D_value2"] = D_value2  ###添加D_value2这一列
    df["yvals2"] = yvals2      ###添加yvals2这一列
    print("df2",df)

    ###求斜率
    XL2 = []
    for pdi in range(0, len(df.H)-1):
        pdi2 = df.iloc[pdi, 13]
        pdi3 = df.iloc[pdi + 1, 13]
        pdj2 = df.iloc[pdi, 1]
        pdj3 = df.iloc[pdi + 1, 1]
        XL = (pdi2 - pdi3) / (pdj2 - pdj3)
        XL2.append(XL)
    df["XL"] = pd.Series(XL2)  ###添加斜率列


 

你可能感兴趣的:(python,list,pandas)