テキスト形式のデータの読み書き
ex1.csv
https://gyazo.com/9b9a57774c750ea4acc6a5324e5e7884
code: Python
df = pd.read_csv('ex1.csv')
df
--------------------------------------------------------------------------
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
--------------------------------------------------------------------------
code: Python
pd.read_table('ex1.csv', sep=',')
--------------------------------------------------------------------------
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
--------------------------------------------------------------------------
ex2.csv
https://gyazo.com/ba99e18eec39e842ac196823420ab8bd
code: Python
pd.read_csv('ex2.csv', header=None)
--------------------------------------------------------------------------
0 1 2 3 4
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
--------------------------------------------------------------------------
code: Python
--------------------------------------------------------------------------
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
--------------------------------------------------------------------------
messages列を、データフレームのインデックスにする。
code: Python
pd.read_csv('ex2.csv', names=names, index_col='message')
--------------------------------------------------------------------------
a b c d
message
hello 1 2 3 4
world 5 6 7 8
foo 9 10 11 12
--------------------------------------------------------------------------
ex3.txt
https://gyazo.com/1e7c8b17f4b3ae773cf1f8a99321045a
正規表現で空白文字で区切る。
code: Python
result = pd.read_table('ex3.txt', sep='\s+')
result
--------------------------------------------------------------------------
A B C
aaa -0.264438 -1.026059 -0.619500
bbb 0.927272 0.302904 -0.032399
ccc -0.264273 -0.386314 -0.217601
ddd -0.871858 -0.348382 1.100491
--------------------------------------------------------------------------
ex4.csv
https://gyazo.com/ca939469c2db9499e1ad88abba309fad
指定列をスキップする。
code: Python
pd.read_csv('ex4.csv', skiprows=0, 2, 3) --------------------------------------------------------------------------
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
--------------------------------------------------------------------------
データフレームをCSVに書き出す。
code: Python
data = pd.read_csv('ex5.csv')
data.to_csv('out.csv')
JSONデータ
JSONをPython形式に変換する。
code: Python
obj = """
{"name": "Wes",
"pet": null,
{"name": "Katie", "age": 38,
}
"""
import json
result = json.loads(obj)
result
--------------------------------------------------------------------------
{'name': 'Wes',
'pet': None,
--------------------------------------------------------------------------
Python形式をJSONに変換する。
code: Python
asjson = json.dumps(result)
asjson
--------------------------------------------------------------------------
'{"name": "Wes", "places_lived": "United States", "Spain", "Germany", "pet": null, "siblings": [{"name": "Scott", "age": 30, "pets": "Zeus", "Zuko"}, {"name": "Katie", "age": 38, "pets": "Sixes", "Stache", "Cisco"}]}' --------------------------------------------------------------------------
code: Python
siblings
--------------------------------------------------------------------------
name age
0 Scott 30
1 Katie 38
--------------------------------------------------------------------------
example.json
https://gyazo.com/1e3d365b58f4ad52e622e41619223a3a
code: Python
data = pd.read_json('example.json')
data
--------------------------------------------------------------------------
a b c
0 1 2 3
1 4 5 6
2 7 8 9
--------------------------------------------------------------------------
code: Python
print(data.to_json())
--------------------------------------------------------------------------
{"a":{"0":1,"1":4,"2":7},"b":{"0":2,"1":5,"2":8},"c":{"0":3,"1":6,"2":9}}
--------------------------------------------------------------------------
code: Python
print(data.to_json(orient='records'))
--------------------------------------------------------------------------
--------------------------------------------------------------------------