3
在處理熊貓數據框內的數據時需要一些幫助。 任何幫助是最受歡迎的。在熊貓數據框中將字符串2.90K轉換爲2900或5.2M到5200000
我有CSV格式的OHCLV數據。我已將文件加載到熊貓數據框中。
如何將體積欄從2.90K轉換爲2900或5.2M轉換爲5200000. 該欄可以包含K形式的千位和M百萬位數。
import pandas as pd
file_path = '/home/fatjoe/UCHM.csv'
df = pd.read_csv(file_path, parse_dates=[0], index_col=0)
df.columns = [
"closing_price",
"opening_price",
"high_price",
"low_price",
"volume",
"change"]
df['opening_price'] = df['closing_price']
df['opening_price'] = df['opening_price'].shift(-1)
df = df.replace('-', 0)
df = df[:-1]
print(df.head())
Console:
Date
2016-09-23 0
2016-09-22 9.60K
2016-09-21 54.20K
2016-09-20 115.30K
2016-09-19 18.90K
2016-09-16 176.10K
2016-09-15 31.60K
2016-09-14 10.00K
2016-09-13 3.20K
謝謝......它的工作原理........現在讓我試着瞭解代碼在做什麼 –
@JosephMNjuguna,歡迎您!我已經爲我的答案添加了一步一步的解釋 - 請檢查... – MaxU
@MaxU ..........現在我知道如何使用正則表達式與熊貓....... ..在它幾天 –