解析 Pandas 数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43432751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing Pandas dataframe
提问by Bardiya Choupani
I have the following data in a single data frame which I parsed from XML
我在从 XML 解析的单个数据框中有以下数据
index xml_data
0 \n
1 sessionKey
2 JKX6G3_07092016_1476953673631
3 \n
4 Number
5 JKX6G3
6 \n
7 CreateDate
8 1468040400000
9 \n
10 Id
11 83737626
12 1
13 \n
14 customerAge
15 64
16 1
I like to make every row after "\n" a column and value associated with the column is the next row for example:
我喜欢将 "\n" 之后的每一行作为一列,与该列关联的值是下一行,例如:
sessionKey Number CreateDate Id Age
JKX6G3_07092016_1476953673631 JKX6G3 1.46804E+12 83737626 64
Is there a more elegant way of doing this than: for row in doc_df.itertuples(): and going through each row and parse?
有没有比以下更优雅的方法: for row in doc_df.itertuples(): 并遍历每一行并解析?
回答by Serenity
import pandas as pd
import numpy as np
# set dataframe
...
# get columns name
columns = []
count_n = 0
for i in range(0, len(df)-1):
if (df.iloc[i]['xml_data'] == '\n'):
columns.append(df.iloc[i+1]['xml_data'])
count_n += 1
# generate new df
new_df = pd.DataFrame(columns = columns, index = np.arange(count_n))
j = 0
count = 0
# set values
for i in range(0, len(df)-2):
if (df.iloc[i]['xml_data'] == '\n'):
new_df.iloc[j][df.iloc[i+1]['xml_data']] = df.iloc[i+2]['xml_data']
count += 1
if count == len(new_df):
count = 0
j += 1
new_df.dropna(inplace=True)
print(new_df)
Result:
结果:
sessionKey Number CreateDate Id customerAge
0 JKX6G3_07092016_1476953673631 JKX6G3 1468040400000 83737626 64
回答by piRSquared
I'd look for the positions of the \n
and add one to locate keys, and add 2 for values. Then build an array and a subsequent dataframe
我会寻找 的位置\n
并添加一个来定位键,并为值添加 2。然后构建一个数组和一个后续的数据框
v = df.xml_data.values
a, b = np.where(v == '\n')[0][None, :] + [[1], [2]]
pd.DataFrame([v[b]], columns=v[a])
sessionKey Number CreateDate Id customerAge
0 JKX6G3_07092016_1476953673631 JKX6G3 1468040400000 83737626 64