Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中读取excel中的行和列,并将它们放入数组中_Python_Arrays_Pandas_Export To Csv_Xlrd - Fatal编程技术网

在Python中读取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

我的目标是读取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.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++