1. pandas读取json文件
import pandas as pd
pathfile = 'xxx.json'
data = pd.read_json(pathfile)
data的类型为<class 'pandas.core.frame.DataFrame'> Python机器学习(八十三)Pandas 读取 JSON 数据
2.pandas日期转换
3.pandas写入csv格式文件出现中文乱码问题解决方法
4. pd.DataFrame
代码:
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))
结果:
0 1 2 3
0 a b c d
1 e f g h
<class 'pandas.core.frame.DataFrame'>
代码:
#对于简单列表而言,转为DataFrame后,需要转置,才是一行
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))
结果:
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'>
- 实际遇到的例子:
某些情况我们需要将一行列表存入csv文件中,当all_content = pd.DataFrame(all_content) 后,数据被转成一列,这时all_content.to_csv() 会出错。所以加一个flag 标志的判断,当是一列是进行转置。
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')
|