Data analysis using python

怪我咯
Release: 2017-04-08 10:54:59
Original
1587 people have browsed it

1: How to parse data in json format

import json,os,sys
current_dir=os.path.abspath(".")

filename=[file for file in os.listdir(current_dir) if ".txt" in file]#得到当前目录中,后缀为.txt的数据文件
fn=filename[0] if len(filename)==1 else "" #从list中取出第一个文件名

if fn: # means we got a valid filename
  fd=open(fn)
  content=[json.loads(line) for line in fd]
  
else:
  print("no txt file in current directory")
  sys.exit(1)
for linedict in content:
  for key,value in linedict.items():
    print(key,value)
  print("\n")
Copy after login


2: Occurrence frequency statistics

import random
from collections import Counter
fruits=[random.choice(["apple","cherry","orange","pear","watermelon","banana"]) for i in range(20)]
print(fruits) #查看所有水果出现的次数

cover_fruits=Counter(fruits)
for fruit,times in cover_fruits.most_common(3):
  print(fruit,times)

########运行结果如下:apple在fruits里出了5次
apple 5  
banana 4
pear 4
Copy after login


3: Method of reloading module py3

import importlib
import.reload(modulename)
Copy after login


4: Which modules are included in pylab

from pylab import *
Copy after login

is equivalent to the following import statement:

from pylab import *
  from numpy import *
  from scipy import *
  import matplotlib
Copy after login

The above is the detailed content of Data analysis using python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template