我有一个“ csv”文件,看起来像这样:
样本数据:
import my-program
while True:
my-program.main()
我想将这一列分成多个,就像这样, 预期结果:
Name : Jai
Age : 25
Address: N P IV
Country:
Name : Jack
Age : 18
Address: T U W IX
Country: USA
谢谢
答案 0 :(得分:1)
首先为DataFrame
用分隔符:\s+
为:
创建2列'columns'
,并用一个或多个空格分隔Age
,然后将第二列转换为numpy数组并重塑4 {{ 1}},通过构造函数创建DataFrame,将第一列的前4个值转换为新列的名称,并在需要时将import pandas as pd
import numpy as np
temp=u"""Name : Jai
Age : 25
Address: N P IV
Country:
Name : Jack
Age : 18
Address: T U W IX
Country: USA"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), sep=":\s+", names=['col1','col2'])
print (df)
col1 col2
0 Name Jai
1 Age 25
2 Address N P IV
3 Country: None
4 Name Jack
5 Age 18
6 Address T U W IX
7 Country USA
列转换为整数:
c = df['col1'].iloc[:4].str.strip(' :')
#pandas 0.24+
df = pd.DataFrame(df['col2'].to_numpy().reshape(-1, 4), columns=c).rename_axis(None, axis=1)
#pandas below 0.24
#df = pd.DataFrame(df['col2'].values.reshape(-1, 4), columns=c).rename_axis(None, axis=1)
df['Age'] = df['Age'].astype(int)
print (df)
Name Age Address Country
0 Jai 25 N P IV None
1 Jack 18 T U W IX USA
foreach (var item in new Uri(urlString).Query.TrimStart('?').Split('&'))
{
var subStrings = item.Split('=');
var key = subStrings[0];
var value = subStrings[1];
// do something with values
}
答案 1 :(得分:0)
如果将数据格式化为JSON,则可以遍历对象并将所有字典项打印到一行上。唯一的问题是使打印出的文本与文本间距正确;排成漂亮的一致的列。
data = [
{'Name':'Jai',
'Age':25,
'Address': 'N P IV',
'Country': 'Canada'},
{'Name' : 'Jack',
'Age' : 18,
'Address': 'T U W IX',
'Country': 'USA'}
]
print("Name\tAge\tAddress\tCountry")
out=""
for adres in data:
for x, y in adres.items():
out=out+str(y)+"\t"
print(out)
out=""
输出的脚本:
Name Age Address Country
Jai 25 N P IV Canada
Jack 18 T U W IX USA