简体   繁体   English

如何将单行 DataFrame 转换为 Python 中的字符串?

[英]How can I convert a single row DataFrame to string in Python?

I have a Pandas DataFrame with only one row (it's been filtered prior).我有一个只有一行的 Pandas DataFrame(之前已过滤)。 For AutoML Tables, I need the inputs in the format shown below - does anyone know the best way to achieve this?对于 AutoML 表,我需要以下格式的输入 - 有谁知道实现此目的的最佳方法?

Pandas Dataframe (Sample): Pandas Dataframe(样品):

Time                 Open     High     Low      Close       Volume
2021-02-23 19:30:00  1.21567  1.21576  1.21483  1.21484     943

AutoML Required Format: AutoML 所需格式:

inputs = {'AverageTrueRange': 0.000697, 'BB_Width': 0.001842, 'CandleRange': 0.00074, 'Close': 1.17512, 'EMA_Gap': -0.00168, 'EntryTime': "2017-11-20 07:30:00", 'MACD_HIST': 0.000329, 'ModFisher': 2.276938, 'ModFisher14': 2.393746, 'PosWithinClose': 0.32, 'PosWithinLowHigh': 0.99, 'RollingVoli': 0.000579, 'RSI': 59.609776, 'Signal': "BEAR SIGNAL", 'Stoch14': 0.991736, 'Volume': 668}

What's the best way to transform the DataFrame into the format I need?将 DataFrame 转换为我需要的格式的最佳方法是什么? It looks kind of dictionary like but there are some discrepancies.它看起来有点像字典,但有一些差异。

Any help would be much appreciated:)任何帮助将非常感激:)

data = [ { "Time": "2021-02-23 19:30:00", "Open": 1.21567, "High": 1.21576, "Low": 1.21483, "Close": 1.21484, "Volume": 943 } ]
df = pd.DataFrame(data)
inputs = df.to_dict('records')[0]

Result:结果:

{'Close': 1.21484, 'High': 1.21576, 'Low': 1.21483, 'Open': 1.21567, 'Time': '2021-02-23 19:30:00', 'Volume': 943}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM