for index, row in df_dir.iterrows():
提取每一行的数:
x_1 = row['A1'] y_1 = row['B1']
对这一行某一列的值进行修改:
df_dir.loc[index,'类型'] = 5
已知某一列的某个数,反推该数的indexs:
indexs = df_temp[(df_temp['dir_factor'] == ref)].index.tolist()
pandas.DataFrame.iterrowsDataFrame.iterrows()
迭代(iterate)覆盖整个DataFrame的行中,返回(index, Series)对。
import numpy as np
import pandas as pd
def _map(data, exp):
for index, row in data.iterrows(): # 获取每行的index、row
for col_name in data.columns:
row[col_name] = exp(row[col_name]) # 把结果返回给data
return data
def _1map(data, exp):
_data = [[exp(row[col_name]) # 把结果转换成2级list
for col_name in data.columns]
for index, row in data.iterrows()
]
return _data
if __name__ == "__main__":
inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]
df = pd.DataFrame(inp)
temp = _map(df, lambda ele: ele+1 )
print temp
_temp = _1map(df, lambda ele: ele+1)
res_data = pd.DataFrame(_temp) # 对2级list转换成DataFrame
print res_data
由于没有看到导出的表格样本,直接说吧:遍历建议直接用pandas的itertuples(),去除前面的空白字符串用lstrip()就行
df = pd.read_excel("test.xlsx")
for row in df.itertuples():
row.行名称=row.行名称.lstrip()
以上应该就可以了,注意缩进。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)