程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Some usage of pandas

編輯:Python

1. pandas Read json file

import pandas as pd
pathfile = 'xxx.json'
data = pd.read_json(pathfile)

data The type of <class 'pandas.core.frame.DataFrame'>
Python machine learning ( eighty-three )Pandas Read JSON data

2.pandas Date conversion

  • Pandas Date time format
  • python datetime unix Timestamp and string timestamp conversion

3.pandas write in csv Solution to the problem of Chinese garbled code in format file

  • utf-8 and utf-8-sig It's different , Often in csv Encountered in file .
  • python3 library pandas write in csv Solution to the problem of Chinese garbled code in format file

4. pd.DataFrame

 Code :
import pandas as pd
a = [['a','b','c','d'], ['e','f','g','h']]
a_df = pd.DataFrame(a)
print(a_df)
print(type(a_df))
result :
0 1 2 3
0 a b c d
1 e f g h
<class 'pandas.core.frame.DataFrame'>
 Code :
# For simple lists , To DataFrame after , Need to transpose , Is a line
b = ['a','b','c','d']
b_df = pd.DataFrame(b)
print(b_df)
print(type(b_df))
b_df_T = b_df.T
print(b_df_T)
print(type(b_df_T))
result :
0
0 a
1 b
2 c
3 d
<class 'pandas.core.frame.DataFrame'>
0 1 2 3
0 a b c d
<class 'pandas.core.frame.DataFrame'>

  • Examples actually encountered :
    In some cases we need to One line list Deposit in csv In file , When all_content = pd.DataFrame(all_content) after , The data is converted into a column , At this time all_content.to_csv() Will make mistakes . So add one flag Judgment of signs , When a column is transposed .
 if len(all_content) == 0:
all_content = row
flag = True
else:
all_content = np.row_stack((all_content, row))
flag = False
all_content = pd.DataFrame(all_content)
if flag:
all_content = all_content.T
all_content.to_csv(out_file, index=False,header=header,encoding='utf-8-sig')

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved