Pandas: 如何更改输出.csv 数据的格式以使用comma-delimiters?
Pandas: How to change the format of output .csv data to use comma-delimiters?
背景
使用 pandas
,以下程序已成功用于将 millisecond-resolution 报价数据重新采样为 1 秒间隔 OHLC(高开低收)数据。从 .csv 文件读取报价数据,程序将 1 秒 OHLC 数据写入具有新列 headers 的新 .csv 文件。问题源于输出数据的格式设置方式(tab-delimited 而不是 comma-delimited,以及不需要的列 headers)。这使得使用 plotly
绘制 OHLC 烛台图表变得具有挑战性。我一直在使用 Sublime Text 3 打开 .csv 文件。
目标:
要1) 确保输出OHLC (Open High Low Close) 数据为comma-delimited,以及2) 只输出时间,不输出今天的日期。可以在下面进一步看到所需的格式,在“.csv 的所需输出格式”下方。
当前尝试:
import pandas as pd
import sys
# Read data from file
data_frame = pd.read_csv('input_data.csv',
names=['Date_time', 'Bid', 'Bid_qty', 'Ask', 'Ask_qty'],
index_col=0, parse_dates=True, delimiter = ',')
data_frame.head()
# Sample data at set intervals
data_bid=data_frame['Bid'].resample('1S').ohlc()
data_ask=data_frame['Ask'].resample('1S').ohlc()
data_bid.head()
data_ask.head()
# Concatenate bid and ask
data_ask_bid = pd.concat(
[data_bid, data_ask], axis=1,
keys=['Bid', 'Ask'])
# Display options
pd.set_option('display.max_rows', None) # Display all rows
pd.set_option('display.max_columns', None) # Display all columns
pd.set_option('display.width', 150) # Fit columns on same line
pd.set_option('display.max_colwidth', None) # Set max width
# Print to file
original_stdout = sys.stdout # Save reference to original standard output
with open('output_data.csv', 'w') as f:
sys.stdout = f # Change the standard output
print(data_ask_bid)
sys.stdout = original_stdout # Reset standard output to original value
对于 pandas.read_csv
函数,尝试使用控制 read_csv 分隔符的参数 sep=','
,但没有成功。还尝试在打印到输出文件时也使用带有参数 sep=','
的 pandas.DataFrame.to_csv
函数,但也不成功。
输入.csv数据格式(时间、出价、买入数量、卖出、卖出数量):
00:00:00.033,10.6674,1000000,10.6724,1000000
00:00:00.060,10.6674,1000000,10.6724,2000000
00:00:01.645,10.6674,1000000,10.6724,1000000
00:00:01.670,10.6674,1000000,10.6724,2000000
00:00:02.198,10.6674,1000000,10.6724,1000000
00:00:03.198,10.6674,1000000,10.672,1000000
00:00:03.202,10.6671,1000000,10.672,1000000
00:00:03.202,10.6672,1000000,10.672,1000000
.csv的当前输出格式:
Bid Ask
open high low close open high low close
Date_time
2020-10-21 00:00:00 10.6674 10.6674 10.6674 10.6674 10.6724 10.6724 10.6724 10.6724
2020-10-21 00:00:01 10.6674 10.6674 10.6674 10.6674 10.6724 10.6724 10.6724 10.6724
2020-10-21 00:00:02 10.6674 10.6674 10.6674 10.6674 10.6724 10.6724 10.6724 10.6724
2020-10-21 00:00:03 10.6674 10.6674 10.6671 10.6672 10.6720 10.6720 10.6720 10.6720
.csv 所需的输出格式:
time,bid_open,bid_high,bid_low,bid_close,ask_open,ask_high,ask_low,ask_close
00:00:00,10.6674,10.6674,10.6674,10.6674,10.6724,10.6724,10.6724,10.6724
00:00:01,10.6674,10.6674,10.6674,10.6674,10.6724,10.6724,10.6724,10.6724
00:00:02,10.6674,10.6674,10.6674,10.6674,10.6724,10.6724,10.6724,10.6724
00:00:03,10.6674,10.6674,10.6671,10.6672,10.6720,10.6720,10.6720,10.6720
总结:
Q1。打印到输出 .csv 文件的每个值(包括 headers 列)如何成为 comma-delimited?
Q2。如何在输出 .csv 文件中显示时间 而没有今天的日期 ?
您似乎将数据帧的字符串表示形式打印到文件中,这是不正确的。如果你这样做它应该工作:
import pandas as pd
# Read data from file
data_frame = pd.read_csv('input_data.csv',
names=['Date_time', 'Bid', 'Bid_qty', 'Ask', 'Ask_qty'],
index_col=0, parse_dates=True, delimiter = ',')
# Sample data at set intervals
data_bid=data_frame['Bid'].resample('1S').ohlc()
data_ask=data_frame['Ask'].resample('1S').ohlc()
# Concatenate bid and ask
data_ask_bid = pd.concat(
[data_bid, data_ask], axis=1,
keys=['Bid', 'Ask'])
data_ask_bid.to_csv('file.csv', index=False)
背景
使用 pandas
,以下程序已成功用于将 millisecond-resolution 报价数据重新采样为 1 秒间隔 OHLC(高开低收)数据。从 .csv 文件读取报价数据,程序将 1 秒 OHLC 数据写入具有新列 headers 的新 .csv 文件。问题源于输出数据的格式设置方式(tab-delimited 而不是 comma-delimited,以及不需要的列 headers)。这使得使用 plotly
绘制 OHLC 烛台图表变得具有挑战性。我一直在使用 Sublime Text 3 打开 .csv 文件。
目标:
要1) 确保输出OHLC (Open High Low Close) 数据为comma-delimited,以及2) 只输出时间,不输出今天的日期。可以在下面进一步看到所需的格式,在“.csv 的所需输出格式”下方。
当前尝试:
import pandas as pd
import sys
# Read data from file
data_frame = pd.read_csv('input_data.csv',
names=['Date_time', 'Bid', 'Bid_qty', 'Ask', 'Ask_qty'],
index_col=0, parse_dates=True, delimiter = ',')
data_frame.head()
# Sample data at set intervals
data_bid=data_frame['Bid'].resample('1S').ohlc()
data_ask=data_frame['Ask'].resample('1S').ohlc()
data_bid.head()
data_ask.head()
# Concatenate bid and ask
data_ask_bid = pd.concat(
[data_bid, data_ask], axis=1,
keys=['Bid', 'Ask'])
# Display options
pd.set_option('display.max_rows', None) # Display all rows
pd.set_option('display.max_columns', None) # Display all columns
pd.set_option('display.width', 150) # Fit columns on same line
pd.set_option('display.max_colwidth', None) # Set max width
# Print to file
original_stdout = sys.stdout # Save reference to original standard output
with open('output_data.csv', 'w') as f:
sys.stdout = f # Change the standard output
print(data_ask_bid)
sys.stdout = original_stdout # Reset standard output to original value
对于 pandas.read_csv
函数,尝试使用控制 read_csv 分隔符的参数 sep=','
,但没有成功。还尝试在打印到输出文件时也使用带有参数 sep=','
的 pandas.DataFrame.to_csv
函数,但也不成功。
输入.csv数据格式(时间、出价、买入数量、卖出、卖出数量):
00:00:00.033,10.6674,1000000,10.6724,1000000
00:00:00.060,10.6674,1000000,10.6724,2000000
00:00:01.645,10.6674,1000000,10.6724,1000000
00:00:01.670,10.6674,1000000,10.6724,2000000
00:00:02.198,10.6674,1000000,10.6724,1000000
00:00:03.198,10.6674,1000000,10.672,1000000
00:00:03.202,10.6671,1000000,10.672,1000000
00:00:03.202,10.6672,1000000,10.672,1000000
.csv的当前输出格式:
Bid Ask
open high low close open high low close
Date_time
2020-10-21 00:00:00 10.6674 10.6674 10.6674 10.6674 10.6724 10.6724 10.6724 10.6724
2020-10-21 00:00:01 10.6674 10.6674 10.6674 10.6674 10.6724 10.6724 10.6724 10.6724
2020-10-21 00:00:02 10.6674 10.6674 10.6674 10.6674 10.6724 10.6724 10.6724 10.6724
2020-10-21 00:00:03 10.6674 10.6674 10.6671 10.6672 10.6720 10.6720 10.6720 10.6720
.csv 所需的输出格式:
time,bid_open,bid_high,bid_low,bid_close,ask_open,ask_high,ask_low,ask_close
00:00:00,10.6674,10.6674,10.6674,10.6674,10.6724,10.6724,10.6724,10.6724
00:00:01,10.6674,10.6674,10.6674,10.6674,10.6724,10.6724,10.6724,10.6724
00:00:02,10.6674,10.6674,10.6674,10.6674,10.6724,10.6724,10.6724,10.6724
00:00:03,10.6674,10.6674,10.6671,10.6672,10.6720,10.6720,10.6720,10.6720
总结:
Q1。打印到输出 .csv 文件的每个值(包括 headers 列)如何成为 comma-delimited?
Q2。如何在输出 .csv 文件中显示时间 而没有今天的日期 ?
您似乎将数据帧的字符串表示形式打印到文件中,这是不正确的。如果你这样做它应该工作:
import pandas as pd
# Read data from file
data_frame = pd.read_csv('input_data.csv',
names=['Date_time', 'Bid', 'Bid_qty', 'Ask', 'Ask_qty'],
index_col=0, parse_dates=True, delimiter = ',')
# Sample data at set intervals
data_bid=data_frame['Bid'].resample('1S').ohlc()
data_ask=data_frame['Ask'].resample('1S').ohlc()
# Concatenate bid and ask
data_ask_bid = pd.concat(
[data_bid, data_ask], axis=1,
keys=['Bid', 'Ask'])
data_ask_bid.to_csv('file.csv', index=False)