Adding a multi index to columns










0















I am attempting to concatenate multiple files together and output to an excel file. My plan was to read the data into a dataframe, perform a few calculations, then write the data to an excel sheet. I would like to add a second label to my dataframe that indicates the file from which it came. I believe that multiindexing is the way to go but I am unsure of how to add.



example of current dataframe:



 readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840


example of intended dataframe:



 file_1 file_2
readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840


Here is the code I am currently using.



# import excel sheet into dataframe
well_reads = pd.read_excel('File.xls', header=0)

# pull positive control and negative control samples into new dataframe
positive_control = well_reads[well_reads['Well'].str.contains('01')]
negative_control = well_reads[well_reads['Well'].str.contains('12')]

# drop postive control and negative control rows from initial dataframe
positive_control_wells = well_reads[well_reads['Well'].str.contains('01')]
index = positive_control_wells.index
well_reads = well_reads.drop(well_reads.index[index])
well_reads = well_reads.reset_index(drop=True)

negative_control_wells = well_reads[well_reads['Well'].str.contains('12')]
index = negative_control_wells.index
well_reads = well_reads.drop(well_reads.index[index])
well_reads = well_reads.reset_index(drop=True)

# Create data frame just containing reads and well id
neutralization_data = well_reads[['CPS (CPS)', 'Well']]

# set index to well id
neutralization_data = neutralization_data.set_index(['Well'])

# identify the geometric mean of the plate
geomean = scipy.stats.gmean(well_reads['CPS (CPS)'])

# identify the IC50 of the plate
IC_50 = geomean/2

# identify the IC80 of the plate
IC_80 = geomean * 0.2


# create a pandas excel writer using xlsxwriter as the engine
writer = pd.ExcelWriter('neutralization data.xlsx', engine='xlsxwriter')

# convert the dataframe to an xlsxwriter excel object
neutralization_data.to_excel(writer, sheet_name='Neutralization Data', startrow=1)

# close the pandas excel writer and output the file
writer.save()









share|improve this question
























  • Hi Morgan, could you add the code you're currently using to write the file?

    – Nathan
    Nov 15 '18 at 20:22











  • The alternative was to save all the file names into a list and then reopen the excel file and write each name to the appropriate cell, but I was trying to get it all in one go.

    – Morgan Gladden
    Nov 15 '18 at 20:34















0















I am attempting to concatenate multiple files together and output to an excel file. My plan was to read the data into a dataframe, perform a few calculations, then write the data to an excel sheet. I would like to add a second label to my dataframe that indicates the file from which it came. I believe that multiindexing is the way to go but I am unsure of how to add.



example of current dataframe:



 readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840


example of intended dataframe:



 file_1 file_2
readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840


Here is the code I am currently using.



# import excel sheet into dataframe
well_reads = pd.read_excel('File.xls', header=0)

# pull positive control and negative control samples into new dataframe
positive_control = well_reads[well_reads['Well'].str.contains('01')]
negative_control = well_reads[well_reads['Well'].str.contains('12')]

# drop postive control and negative control rows from initial dataframe
positive_control_wells = well_reads[well_reads['Well'].str.contains('01')]
index = positive_control_wells.index
well_reads = well_reads.drop(well_reads.index[index])
well_reads = well_reads.reset_index(drop=True)

negative_control_wells = well_reads[well_reads['Well'].str.contains('12')]
index = negative_control_wells.index
well_reads = well_reads.drop(well_reads.index[index])
well_reads = well_reads.reset_index(drop=True)

# Create data frame just containing reads and well id
neutralization_data = well_reads[['CPS (CPS)', 'Well']]

# set index to well id
neutralization_data = neutralization_data.set_index(['Well'])

# identify the geometric mean of the plate
geomean = scipy.stats.gmean(well_reads['CPS (CPS)'])

# identify the IC50 of the plate
IC_50 = geomean/2

# identify the IC80 of the plate
IC_80 = geomean * 0.2


# create a pandas excel writer using xlsxwriter as the engine
writer = pd.ExcelWriter('neutralization data.xlsx', engine='xlsxwriter')

# convert the dataframe to an xlsxwriter excel object
neutralization_data.to_excel(writer, sheet_name='Neutralization Data', startrow=1)

# close the pandas excel writer and output the file
writer.save()









share|improve this question
























  • Hi Morgan, could you add the code you're currently using to write the file?

    – Nathan
    Nov 15 '18 at 20:22











  • The alternative was to save all the file names into a list and then reopen the excel file and write each name to the appropriate cell, but I was trying to get it all in one go.

    – Morgan Gladden
    Nov 15 '18 at 20:34













0












0








0








I am attempting to concatenate multiple files together and output to an excel file. My plan was to read the data into a dataframe, perform a few calculations, then write the data to an excel sheet. I would like to add a second label to my dataframe that indicates the file from which it came. I believe that multiindexing is the way to go but I am unsure of how to add.



example of current dataframe:



 readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840


example of intended dataframe:



 file_1 file_2
readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840


Here is the code I am currently using.



# import excel sheet into dataframe
well_reads = pd.read_excel('File.xls', header=0)

# pull positive control and negative control samples into new dataframe
positive_control = well_reads[well_reads['Well'].str.contains('01')]
negative_control = well_reads[well_reads['Well'].str.contains('12')]

# drop postive control and negative control rows from initial dataframe
positive_control_wells = well_reads[well_reads['Well'].str.contains('01')]
index = positive_control_wells.index
well_reads = well_reads.drop(well_reads.index[index])
well_reads = well_reads.reset_index(drop=True)

negative_control_wells = well_reads[well_reads['Well'].str.contains('12')]
index = negative_control_wells.index
well_reads = well_reads.drop(well_reads.index[index])
well_reads = well_reads.reset_index(drop=True)

# Create data frame just containing reads and well id
neutralization_data = well_reads[['CPS (CPS)', 'Well']]

# set index to well id
neutralization_data = neutralization_data.set_index(['Well'])

# identify the geometric mean of the plate
geomean = scipy.stats.gmean(well_reads['CPS (CPS)'])

# identify the IC50 of the plate
IC_50 = geomean/2

# identify the IC80 of the plate
IC_80 = geomean * 0.2


# create a pandas excel writer using xlsxwriter as the engine
writer = pd.ExcelWriter('neutralization data.xlsx', engine='xlsxwriter')

# convert the dataframe to an xlsxwriter excel object
neutralization_data.to_excel(writer, sheet_name='Neutralization Data', startrow=1)

# close the pandas excel writer and output the file
writer.save()









share|improve this question
















I am attempting to concatenate multiple files together and output to an excel file. My plan was to read the data into a dataframe, perform a few calculations, then write the data to an excel sheet. I would like to add a second label to my dataframe that indicates the file from which it came. I believe that multiindexing is the way to go but I am unsure of how to add.



example of current dataframe:



 readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840


example of intended dataframe:



 file_1 file_2
readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840


Here is the code I am currently using.



# import excel sheet into dataframe
well_reads = pd.read_excel('File.xls', header=0)

# pull positive control and negative control samples into new dataframe
positive_control = well_reads[well_reads['Well'].str.contains('01')]
negative_control = well_reads[well_reads['Well'].str.contains('12')]

# drop postive control and negative control rows from initial dataframe
positive_control_wells = well_reads[well_reads['Well'].str.contains('01')]
index = positive_control_wells.index
well_reads = well_reads.drop(well_reads.index[index])
well_reads = well_reads.reset_index(drop=True)

negative_control_wells = well_reads[well_reads['Well'].str.contains('12')]
index = negative_control_wells.index
well_reads = well_reads.drop(well_reads.index[index])
well_reads = well_reads.reset_index(drop=True)

# Create data frame just containing reads and well id
neutralization_data = well_reads[['CPS (CPS)', 'Well']]

# set index to well id
neutralization_data = neutralization_data.set_index(['Well'])

# identify the geometric mean of the plate
geomean = scipy.stats.gmean(well_reads['CPS (CPS)'])

# identify the IC50 of the plate
IC_50 = geomean/2

# identify the IC80 of the plate
IC_80 = geomean * 0.2


# create a pandas excel writer using xlsxwriter as the engine
writer = pd.ExcelWriter('neutralization data.xlsx', engine='xlsxwriter')

# convert the dataframe to an xlsxwriter excel object
neutralization_data.to_excel(writer, sheet_name='Neutralization Data', startrow=1)

# close the pandas excel writer and output the file
writer.save()






python pandas multi-index xlsxwriter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:32







Morgan Gladden

















asked Nov 15 '18 at 20:20









Morgan GladdenMorgan Gladden

398




398












  • Hi Morgan, could you add the code you're currently using to write the file?

    – Nathan
    Nov 15 '18 at 20:22











  • The alternative was to save all the file names into a list and then reopen the excel file and write each name to the appropriate cell, but I was trying to get it all in one go.

    – Morgan Gladden
    Nov 15 '18 at 20:34

















  • Hi Morgan, could you add the code you're currently using to write the file?

    – Nathan
    Nov 15 '18 at 20:22











  • The alternative was to save all the file names into a list and then reopen the excel file and write each name to the appropriate cell, but I was trying to get it all in one go.

    – Morgan Gladden
    Nov 15 '18 at 20:34
















Hi Morgan, could you add the code you're currently using to write the file?

– Nathan
Nov 15 '18 at 20:22





Hi Morgan, could you add the code you're currently using to write the file?

– Nathan
Nov 15 '18 at 20:22













The alternative was to save all the file names into a list and then reopen the excel file and write each name to the appropriate cell, but I was trying to get it all in one go.

– Morgan Gladden
Nov 15 '18 at 20:34





The alternative was to save all the file names into a list and then reopen the excel file and write each name to the appropriate cell, but I was trying to get it all in one go.

– Morgan Gladden
Nov 15 '18 at 20:34












1 Answer
1






active

oldest

votes


















2














Like you said, adding the multi-index columns will solve your issue before you write the output:



df=pd.DataFrame(0:[1.098,3.185,0.938, 5.283],1:[4.514,2.124,0.369, 7.840])
df.columns=pd.MultiIndex.from_tuples([('file1','readout'),('file2','readout')])


gives



 file1 file2
readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840





share|improve this answer























  • Thanks! Works perfectly.

    – Morgan Gladden
    Nov 15 '18 at 21:45











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327312%2fadding-a-multi-index-to-columns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Like you said, adding the multi-index columns will solve your issue before you write the output:



df=pd.DataFrame(0:[1.098,3.185,0.938, 5.283],1:[4.514,2.124,0.369, 7.840])
df.columns=pd.MultiIndex.from_tuples([('file1','readout'),('file2','readout')])


gives



 file1 file2
readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840





share|improve this answer























  • Thanks! Works perfectly.

    – Morgan Gladden
    Nov 15 '18 at 21:45















2














Like you said, adding the multi-index columns will solve your issue before you write the output:



df=pd.DataFrame(0:[1.098,3.185,0.938, 5.283],1:[4.514,2.124,0.369, 7.840])
df.columns=pd.MultiIndex.from_tuples([('file1','readout'),('file2','readout')])


gives



 file1 file2
readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840





share|improve this answer























  • Thanks! Works perfectly.

    – Morgan Gladden
    Nov 15 '18 at 21:45













2












2








2







Like you said, adding the multi-index columns will solve your issue before you write the output:



df=pd.DataFrame(0:[1.098,3.185,0.938, 5.283],1:[4.514,2.124,0.369, 7.840])
df.columns=pd.MultiIndex.from_tuples([('file1','readout'),('file2','readout')])


gives



 file1 file2
readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840





share|improve this answer













Like you said, adding the multi-index columns will solve your issue before you write the output:



df=pd.DataFrame(0:[1.098,3.185,0.938, 5.283],1:[4.514,2.124,0.369, 7.840])
df.columns=pd.MultiIndex.from_tuples([('file1','readout'),('file2','readout')])


gives



 file1 file2
readout readout
0 1.098 4.514
1 3.185 2.124
2 0.938 0.369
3 5.283 7.840






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 20:40









kevins_1kevins_1

3431213




3431213












  • Thanks! Works perfectly.

    – Morgan Gladden
    Nov 15 '18 at 21:45

















  • Thanks! Works perfectly.

    – Morgan Gladden
    Nov 15 '18 at 21:45
















Thanks! Works perfectly.

– Morgan Gladden
Nov 15 '18 at 21:45





Thanks! Works perfectly.

– Morgan Gladden
Nov 15 '18 at 21:45



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327312%2fadding-a-multi-index-to-columns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3