python - Create a dataframe from a list with multiple columns
问题描述
I want to create a dataframe from a list, the thing is that my column name is also in the list.
List:
['Input_file_column_name,Is_key,Config_file_column_name,Value\nEmployee ID,Y,identifierValue,identityTypeCode:001\nCumb ID,N,identifierValue,identityTypeCode:002\nFirst Name,N,first_Name \nLast Name,N,last_Name \nEmail,N,email_Address \nEntityID,N,entity_Id,entity_Id:01\nSourceCode,N,sourceCode,sourceCode:AHRWB\n']
Resulting dataframe:
Input_file_column_name Is_key Config_file_column_name Value
0 Employee ID Y identifierValue identityTypeCode:001
1 Cumb ID N identifierValue identityTypeCode:002
5 EntityID N entity_Id entity_Id:01
6 SourceCode N sourceCode sourceCode:AHRWB
How do I convert it? Do I convert the list to a dictionary and then do it or is there a way that it can be done directly?
Code:
import pandas as pd
with open('onboard_config.txt') as myFile:
text = myFile.read()
result = text.split("regex")
print result
df=pd.DataFrame[[sub.split(",") for sub in result]]
解决方案
似乎您需要splitlines
然后转换为Series.str.split
df=pd.Series(l[0].splitlines()).str.split(',',expand=True).T.set_index(0).T.dropna()
df
Out[1183]:
0 Input_file_column_name ... Value
1 Employee ID ... identityTypeCode:001
2 Cumb ID ... identityTypeCode:002
6 EntityID ... entity_Id:01
7 SourceCode ... sourceCode:AHRWB
[4 rows x 4 columns]
推荐阅读
- javascript - Javascript 函数对象(从函数对象内部的函数调用属性)
- ios - 增加可点击的 Textfield() 大小
- javascript - 我怎样才能使这个 JQuery 代码更具响应性?
- java - 返回一个列表
在java中 - azure - Azure LogicApp 是否支持 WebDav 连接?
- python - numpy 日志溢出记录在哪里?
- python - Matplotlib dateformatter 未按要求格式化日期
- reactjs - 在使用 useEffect 挂钩时,我在浏览器上收到此错误
- flutter - 调用 Navigator.of(context).pop() 后如何获取父上下文?
- google-apps-script - Google表格脚本:查找包含在公式中的文本的函数