简体   繁体   English

读取具有键值对的文本文件,并使用python pandas将每一行转换为一个字典

[英]read a text file which has key value pairs and convert each line as one dictionary using python pandas

I have a text file (one.txt) that contains an arbitrary number of key‐value pairs (where the key and value are separated by a = – eg 1=8 ). 我有一个文本文件(one.txt),其中包含任意数量的键/值对(键和值之间用a = –例如1=8分隔)。 Here are some examples: 这里有些例子:

1=88|11=1438|15=KKK|45=00|45=00|21=66|86=a
4=13|11=1438|49=DDD|8=157.73|67=00|45=00|84=b|86=a
6=84|41=18|56=TTT|67=00|4=13|45=00|07=d

I need to create a DataFrame with a list of dictionaries, with each row as one dictionary in the list like so: 我需要创建一个带有字典列表的DataFrame,并将每一行作为列表中的一个字典,如下所示:

[{1:88,11:1438,15:kkk,45:7.7....},{4:13,11:1438....},{6:84,41:18,56:TTT...}]

df = pd.read_csv("input.txt",names=['text'],header=None)
data = df['text'].str.split("|")
names=[  y.split('=') for x in data for y in x]
ds=pd.DataFrame(names)
print ds

How can I create a dictionary for each line by splitting on the = symbol? 如何通过分割=符号为每行创建一个词典?

It should be one row and multiple columns. 它应该是一行和多列。 The DataFrame should have all keys as rows and values as columns. DataFrame应将所有键作为行,将值作为列。

Example: 例:

1 11 15 45 21 86 4 49 8 67 84 6 41 56 45 07
88 1438 kkk 00 66 a
na 1438 na .....

I think performing a .pivot would work. 我认为执行.pivot会起作用。 Try this: 尝试这个:

import pandas as pd

df = pd.read_csv("input.txt",names=['text'],header=None)
data = df['text'].str.split("|")
names=[  y.split('=') for x in data for y in x]
ds=pd.DataFrame(names)
ds = ds.pivot(columns=0).fillna('')

The .fillna('') removes the None values. .fillna('')删除None值。 If you'd like to replace with na you can use .fillna('na') . 如果要替换为na ,可以使用.fillna('na')

Output: 输出:

ds.head()

   1
0 07   1    11   15 21 4 41  45 49 56 6 67 8 84 86
0     88
1         1438
2               KKK
3                            00
4                            00

For space I didn't print the entire dataframe, but it does column indexing based on the key and then values based on the values for each line (preserving the dict by line concept). 对于空间,我没有打印整个数据框,但它会根据键进行列索引,然后根据每行的值进行值索引(保留逐行字典)。

暂无
暂无

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

相关问题 Python:当每个键和值都在新行上时,如何将文本文件读入字典? - Python: How to read a text file into a dictionary when each key and value is on a new line? 在python中将文本片段转换为字典键值对/ JSON - Convert text snippets to dictionary key value pairs/JSON in python Python:读取一个文本文件(每行都是字典结构)到字典 - Python: read a text file (each line is of dictionary structure) to a dictionary 将字典转换为具有键值对的 python 数据帧 - Convert dictionary to python dataframe which has key value pair 如何将多键字典转换为 pandas dataframe,其中每个键和值都有自己的列? - How to convert a multi-key dictionary to a pandas dataframe, where each key and value has its own column? Python 逐行读取文件并转换为字典 - Python read file line by line and convert to dictionary 如何将熊猫列作为键值对添加到现有的python字典 - how to add pandas columns as key value pairs to existing python dictionary 将文件中的连续两行作为值,密钥对读取到字典中 - Read two consecutive lines from a file into a dictionary as value, key pairs 将从文件中读取的行拆分为字典键值对 - Splitting lines read from file into dictionary key-value pairs 使用文本文件创建字典时出现问题,该字典以字长为键,实际字本身为 Python 中的值 - Problem with using a text file to create a dictionary that has word length as its key and the actual word itself as the value in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM