在Python中读取excel中的行和列,并将它们放入数组中
我的目标是读取excel数据,然后将每个名字分别归类为名字、第二个名字分别归类为第二个名字和域分别归类为域变量。您可以使用在Python中读取excel中的行和列,并将它们放入数组中,python,arrays,pandas,export-to-csv,xlrd,Python,Arrays,Pandas,Export To Csv,Xlrd,我的目标是读取excel数据,然后将每个名字分别归类为名字、第二个名字分别归类为第二个名字和域分别归类为域变量。您可以使用pandas迭代行,更新数据,然后使用pandas再次将其保存到excel: import pandas as pd df = pd.read_excel('input.xlsx', index_col=None) output = {'0': [], '1': [], '2': [], '3': [], '4': []} for index, row in df.ite
pandas
迭代行,更新数据,然后使用pandas
再次将其保存到excel:
import pandas as pd
df = pd.read_excel('input.xlsx', index_col=None)
output = {'0': [], '1': [], '2': [], '3': [], '4': []}
for index, row in df.iterrows():
output['0'].append(f"{row['First']}@{row['Domain']}")
output['1'].append(f"{row['Second']}@{row['Domain']}")
output['2'].append(f"{row['First']}{row['Second']}@{row['Domain']}")
output['3'].append(f"{row['First']}.{row['Second']}@{row['Domain']}")
output['4'].append(f"{row['First'][0]}{row['Second']}@{row['Domain']}")
df = pd.DataFrame(output, columns=list(output.keys()))
df.to_excel('output.xlsx')
输出:
我知道你想要这样的东西:
df = pandas.read_excel("input.xlsx")
def generate(data):
first,last,domain = data
return [ fl+'@'+domain for fl in \
[first,last,first+last,first+'.'+last,first[0]+last]]
df.apply(generate,'columns',result_type='expand').to_excel("output.xlsx")
这样做的好功能是。
generate
的参数必须是一行对应的序列。谢谢!但是如果有10000多行,这不是效率很低吗?我不需要初始化10k阵列吗。有更快的方法吗?对不起,忘了给你贴标签了对不起,我不知道更快的方法。可能使用C++