本文整理汇总了Python中pandas.DataFrame方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.DataFrame方法的具体用法?Python pandas.DataFrame怎么用?Python pandas.DataFrame使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas
的用法示例。
在下文中一共展示了pandas.DataFrame方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: average_true_range
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def average_true_range(df, n):
"""
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
i = 0
TR_l = [0]
while i < df.index[-1]:
TR = max(df.loc[i + 1, 'High'], df.loc[i, 'Close']) - min(df.loc[i + 1, 'Low'], df.loc[i, 'Close'])
TR_l.append(TR)
i = i + 1
TR_s = pd.Series(TR_l)
ATR = pd.Series(TR_s.ewm(span=n, min_periods=n).mean(), name='ATR_' + str(n))
df = df.join(ATR)
return df
示例2: create_lines
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def create_lines(self, x, varieties):
"""
Draw just the data portion.
"""
lines = pd.DataFrame()
for i, var in enumerate(varieties):
self.legend.append(var)
data = varieties[var]["data"]
color = get_color(varieties[var], i)
x_array = np.array(x)
y_array = np.array(data)
line = pd.DataFrame({"x": x_array,
"y": y_array,
"color": color,
"var": var})
lines = lines.append(line, ignore_index=True, sort=False)
return lines
示例3: create_scats
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def create_scats(self, varieties):
self.scats = pd.DataFrame(columns=["x", "y", "color", "marker", "var"])
for i, var in enumerate(varieties):
self.legend.append(var)
(x_array, y_array) = self.get_arrays(varieties, var)
if len(x_array) <= 0: # no data to graph!
'''
I am creating a single "position" for an agent that cannot
be seen. This seems to fix the issue of colors being
missmatched in the occasion that a group has no agents.
'''
x_array = [-1]
y_array = [-1]
elif len(x_array) != len(y_array):
logging.debug("Array length mismatch in scatter plot")
return
color = get_color(varieties[var], i)
marker = get_marker(varieties[var], i)
scat = pd.DataFrame({"x": pd.Series(x_array),
"y": pd.Series(y_array),
"color": color,
"marker": marker,
"var": var})
self.scats = self.scats.append(scat, ignore_index=True,
sort=False)
示例4: run
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def run(which):
print("running all tests...")
summary = pandas.DataFrame(columns=["pass", "info", "timing"])
# Test chromosome ends
if len(which)==0 or "chrom_ends" in which:
summary.loc["chrom_ends"] = _runTest(runTestIssues, "issues")
# Run the demos
if len(which)==0 or "demos" in which:
summary.loc["demos"] = _runTest(testDemos.run, "demos")
# Run regression testing on ref/alt/amb counts
if len(which)==0 or "counts" in which:
summary.loc["counts"] = _runTest(runTestCounts, "counts")
# Run the render regression tests
if len(which)==0 or "rendering" in which:
summary.loc["rendering"] = _runTest(rendertest.run, "rendering")
summary["timing"] = summary["timing"].apply(lambda x: "{}".format(datetime.timedelta(seconds=int(x))))
print(summary)
saveTimingInfo(summary)
示例5: write_daily_alert
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def write_daily_alert(cls, symbol, stock_id, action):
"""
write daily stock alert to MongoDB.
:param symbol: Arctic symbol
:param data: dict, like: {'stock': '000651', 'action': 'buy/sell'}
:return: None
"""
lib = get_or_create_library(conf.DAILY_STOCK_ALERT_LIBNAME)
data = {
'stock': stock_id,
'action': action
}
df = pd.DataFrame([data], columns=data.keys())
if symbol in lib.list_symbols():
lib.append(symbol, df)
else:
lib.write(symbol, df)
示例6: _test_strip_unused_cols
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def _test_strip_unused_cols(self):
data = pd.DataFrame({
'name': ['tom', 'jack'],
'age': [24, 56],
'gender': ['male', 'male'],
'address': ['cn', 'us']
})
data.index = pd.date_range(start='2017-01-01', periods=2)
origin_cols = ['name', 'age', 'gender', 'address']
unused_cols = ['address', 'gender']
new_cols = ['name', 'age']
self.assertEqual(list(data.columns).sort(), origin_cols.sort())
bdu.Utils.strip_unused_cols(data, *unused_cols)
self.assertEqual(list(data.columns).sort(), new_cols.sort())
示例7: ppsr
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def ppsr(df):
"""Calculate Pivot Points, Supports and Resistances for given data
:param df: pandas.DataFrame
:return: pandas.DataFrame
"""
PP = pd.Series((df['High'] + df['Low'] + df['Close']) / 3)
R1 = pd.Series(2 * PP - df['Low'])
S1 = pd.Series(2 * PP - df['High'])
R2 = pd.Series(PP + df['High'] - df['Low'])
S2 = pd.Series(PP - df['High'] + df['Low'])
R3 = pd.Series(df['High'] + 2 * (PP - df['Low']))
S3 = pd.Series(df['Low'] - 2 * (df['High'] - PP))
psr = {'PP': PP, 'R1': R1, 'S1': S1, 'R2': R2, 'S2': S2, 'R3': R3, 'S3': S3}
PSR = pd.DataFrame(psr)
df = df.join(PSR)
return df
示例8: trix
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def trix(df, n):
"""Calculate TRIX for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
EX1 = df['Close'].ewm(span=n, min_periods=n).mean()
EX2 = EX1.ewm(span=n, min_periods=n).mean()
EX3 = EX2.ewm(span=n, min_periods=n).mean()
i = 0
ROC_l = [np.nan]
while i + 1 <= df.index[-1]:
ROC = (EX3[i + 1] - EX3[i]) / EX3[i]
ROC_l.append(ROC)
i = i + 1
Trix = pd.Series(ROC_l, name='Trix_' + str(n))
df = df.join(Trix)
return df
示例9: vortex_indicator
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def vortex_indicator(df, n):
"""Calculate the Vortex Indicator for given data.
Vortex Indicator described here:
http://www.vortexindicator.com/VFX_VORTEX.PDF
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
i = 0
TR = [0]
while i < df.index[-1]:
Range = max(df.loc[i + 1, 'High'], df.loc[i, 'Close']) - min(df.loc[i + 1, 'Low'], df.loc[i, 'Close'])
TR.append(Range)
i = i + 1
i = 0
VM = [0]
while i < df.index[-1]:
Range = abs(df.loc[i + 1, 'High'] - df.loc[i, 'Low']) - abs(df.loc[i + 1, 'Low'] - df.loc[i, 'High'])
VM.append(Range)
i = i + 1
VI = pd.Series(pd.Series(VM).rolling(n).sum() / pd.Series(TR).rolling(n).sum(), name='Vortex_' + str(n))
df = df.join(VI)
return df
示例10: true_strength_index
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def true_strength_index(df, r, s):
"""Calculate True Strength Index (TSI) for given data.
:param df: pandas.DataFrame
:param r:
:param s:
:return: pandas.DataFrame
"""
M = pd.Series(df['Close'].diff(1))
aM = abs(M)
EMA1 = pd.Series(M.ewm(span=r, min_periods=r).mean())
aEMA1 = pd.Series(aM.ewm(span=r, min_periods=r).mean())
EMA2 = pd.Series(EMA1.ewm(span=s, min_periods=s).mean())
aEMA2 = pd.Series(aEMA1.ewm(span=s, min_periods=s).mean())
TSI = pd.Series(EMA2 / aEMA2, name='TSI_' + str(r) + '_' + str(s))
df = df.join(TSI)
return df
示例11: money_flow_index
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def money_flow_index(df, n):
"""Calculate Money Flow Index and Ratio for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
PP = (df['High'] + df['Low'] + df['Close']) / 3
i = 0
PosMF = [0]
while i < df.index[-1]:
if PP[i + 1] > PP[i]:
PosMF.append(PP[i + 1] * df.loc[i + 1, 'Volume'])
else:
PosMF.append(0)
i = i + 1
PosMF = pd.Series(PosMF)
TotMF = PP * df['Volume']
MFR = pd.Series(PosMF / TotMF)
MFI = pd.Series(MFR.rolling(n, min_periods=n).mean(), name='MFI_' + str(n))
df = df.join(MFI)
return df
示例12: on_balance_volume
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def on_balance_volume(df, n):
"""Calculate On-Balance Volume for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
i = 0
OBV = [0]
while i < df.index[-1]:
if df.loc[i + 1, 'Close'] - df.loc[i, 'Close'] > 0:
OBV.append(df.loc[i + 1, 'Volume'])
if df.loc[i + 1, 'Close'] - df.loc[i, 'Close'] == 0:
OBV.append(0)
if df.loc[i + 1, 'Close'] - df.loc[i, 'Close'] < 0:
OBV.append(-df.loc[i + 1, 'Volume'])
i = i + 1
OBV = pd.Series(OBV)
OBV_ma = pd.Series(OBV.rolling(n, min_periods=n).mean(), name='OBV_' + str(n))
df = df.join(OBV_ma)
return df
示例13: coppock_curve
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def coppock_curve(df, n):
"""Calculate Coppock Curve for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
M = df['Close'].diff(int(n * 11 / 10) - 1)
N = df['Close'].shift(int(n * 11 / 10) - 1)
ROC1 = M / N
M = df['Close'].diff(int(n * 14 / 10) - 1)
N = df['Close'].shift(int(n * 14 / 10) - 1)
ROC2 = M / N
Copp = pd.Series((ROC1 + ROC2).ewm(span=n, min_periods=n).mean(), name='Copp_' + str(n))
df = df.join(Copp)
return df
示例14: keltner_channel
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def keltner_channel(df, n):
"""Calculate Keltner Channel for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
KelChM = pd.Series(((df['High'] + df['Low'] + df['Close']) / 3).rolling(n, min_periods=n).mean(),
name='KelChM_' + str(n))
KelChU = pd.Series(((4 * df['High'] - 2 * df['Low'] + df['Close']) / 3).rolling(n, min_periods=n).mean(),
name='KelChU_' + str(n))
KelChD = pd.Series(((-2 * df['High'] + 4 * df['Low'] + df['Close']) / 3).rolling(n, min_periods=n).mean(),
name='KelChD_' + str(n))
df = df.join(KelChM)
df = df.join(KelChU)
df = df.join(KelChD)
return df
示例15: ultimate_oscillator
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import DataFrame [as 别名]
def ultimate_oscillator(df):
"""Calculate Ultimate Oscillator for given data.
:param df: pandas.DataFrame
:return: pandas.DataFrame
"""
i = 0
TR_l = [0]
BP_l = [0]
while i < df.index[-1]:
TR = max(df.loc[i + 1, 'High'], df.loc[i, 'Close']) - min(df.loc[i + 1, 'Low'], df.loc[i, 'Close'])
TR_l.append(TR)
BP = df.loc[i + 1, 'Close'] - min(df.loc[i + 1, 'Low'], df.loc[i, 'Close'])
BP_l.append(BP)
i = i + 1
UltO = pd.Series((4 * pd.Series(BP_l).rolling(7).sum() / pd.Series(TR_l).rolling(7).sum()) + (
2 * pd.Series(BP_l).rolling(14).sum() / pd.Series(TR_l).rolling(14).sum()) + (
pd.Series(BP_l).rolling(28).sum() / pd.Series(TR_l).rolling(28).sum()),
name='Ultimate_Osc')
df = df.join(UltO)
return df