简体   繁体   English

包含字符串和数字的列表,将每个字符串和数字放在自己的单元格中

[英]lists that contain strings and numbers to excel and put each in its own cell

I have the following list:我有以下清单:

["Test1 10 15 Top Gain",
 "Test2 11 12 Top Gain"
 "Test3 12 15 Top Gain"]

and I want to take this from Python to excel and have it come up like this in excel.我想把它从 Python 带到 excel 并让它像这样在 excel 中出现。

  A      B   C   D
1 Test1  10  15  Top Gain
2 Test2  11  12  Top Gain
3 Test3  12  15  Top Gain

You can parse the list data into dictionary format, then you can construct a dataframe using dictionary.您可以将list数据解析为字典格式,然后您可以使用字典构造一个dataframe Once you have the dictionary, to_excel() does the exporing.一旦你有了字典, to_excel()进行曝光。

import pandas as pd

L = ["Test1 10 15 Top_Gain",
     "Test2 11 12 Top_Gain",
     "Test3 12 15 Top_Gain"]

data_dict = {}
for i, val in enumerate(L):
    data_dict[i] = val.split()

data = pd.DataFrame(data_dict)
data = data.T
data.columns = ['A', 'B', 'C', 'D']
print(data)
#        A   B   C         D
# 0  Test1  10  15  Top_Gain
# 1  Test2  11  12  Top_Gain
# 2  Test3  12  15  Top_Gain


data.to_excel('MyExcelFile.xlsx')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每个子类都包含自己的列表吗? - Have each subclass contain a list of its own? 如何在列表列表中找到具有最大值的列表(嵌套列表包含字符串和数字)? - How to find the lists with max values in a list of lists (where nested lists contain strings and numbers)? 有没有办法将字符串列表中的每个字符串变成自己的空列表? - Is there a way to make each string from a list of strings into their own empty lists? 写入CSV会导致每个字母都有其自己的单元格 - Writing to CSV results in each letter having its own cell Python:将每个 excel 行打印为自己的 ms word 页面 - Python: print each excel row as its own ms word page 从多个列表中选择随机数,它必须至少包含每个列表中的一个数字 - Choose random numbers from multiple lists and it must contain at least one number from each list 如何将 Pandas Dataframe 与 Numpy 列表作为单个列转换为每个索引代表其自己的列? - How to turn a Pandas Dataframe with Numpy Lists as a single column into having each index represent its own column? 如何枚举字符串列表,将每个字符串转换为它自己的列表? - How Do You Enumerate Over a List of Strings, Converting Each String to Its Own List? 如何在Python 2.7.2中获取一串数字并使每个数字成为列表中自己的元素? - How do I take a string of numbers in Python 2.7.2 and make each number its own element in a list? 如何对包含字母和数字的列表进行排序? - How to sort lists that contain letters and numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM