[英]txt file convert into dataframe
I have a txt file with three features:我有一个具有三个功能的 txt 文件:
file_name.txt:
[{"x":1599235200000,"isValid":true,"y":10830027},
{"x":1599321600000,"isValid":true,"y":10883502},
{"x":1599408000000,"isValid":true,"y":10915511}]
Using python read the txt file and make it into dataframe.使用python读取txt文件并将其放入数据帧。
The output I wish I have is only x and y: x is the timestamp and should be 1599235200.000 --> (2020, 9, 4, 16, 0) y is the numerical value, and the output should convert into the dataframe.我希望的输出只有 x 和 y:x 是时间戳,应该是 1599235200.000 --> (2020, 9, 4, 16, 0) y 是数值,输出应该转换为数据帧。
dataframe:
x y
2020-09-04 10830027
2020-09-05 10883502
2020-09-06 10915511
It looks like JSON data.它看起来像 JSON 数据。 pandas has a method
read_json
to read JSON files. pandas 有一个
read_json
方法来读取 JSON 文件。
df = pd.read_json('file_name.txt')
df['x'] = pd.to_datetime(df['x'])
df
Output:输出:
x isValid y
0 1970-01-01 00:26:39.235200 True 10830027
1 1970-01-01 00:26:39.321600 True 10883502
2 1970-01-01 00:26:39.408000 True 10915511
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.