[英]Extracting pandas dataframe from another dataframe
Suppose i have the following dataframe:假设我有以下 dataframe:
Date Open High Low Close Volume min max Loc
Date
2020-06-15 14:00:00 2020-06-15 14:00:00 0.000123 0.000130 0.000121 0.000128 1467828.0 0.00012081 0.00013040 0
2020-06-15 18:00:00 2020-06-15 18:00:00 0.000128 0.000129 0.000123 0.000125 1264642.0 0 0 1
2020-06-15 22:00:00 2020-06-15 22:00:00 0.000125 0.000126 0.000122 0.000123 723738.0 0 0 2
I'm trying to create a new dataframe where:我正在尝试创建一个新的 dataframe ,其中:
Open
, min
, max
Loc
but ONLY where min
and max
are > 0.数据应该是Open
、 min
、 max
Loc
列,但仅限于min
和max
> 0 的情况。Loc
dataframe 的索引应该是列Loc
Now i know that to create a Dataframe from another dataframe i can use pandas.concat()
but i don't know how to set the conditions i explained above.现在我知道要从另一个 dataframe 创建 Dataframe 我可以使用pandas.concat()
但我不知道如何设置上面解释的条件。 Can anyone help me out on this?谁能帮我解决这个问题?
Expected output example:预期的 output 示例:
Loc Open min max
0 0.000123 0.00012081 0.00013040
First filter by mask created by DataFrame.gt
for compare for greater of both columns with DataFrame.all
, select columns by DataFrame.loc
and last add DataFrame.set_index
: First filter by mask created by DataFrame.gt
for compare for greater of both columns with DataFrame.all
, select columns by DataFrame.loc
and last add DataFrame.set_index
:
df = df.loc[df[['min','max']].gt(0).all(axis=1), ['Open','min','max','Loc']].set_index('Loc')
print (df)
Open min max
Loc
0 0.000123 0.000121 0.00013
Or compare both columns separately and chain masks by &
for bitwise AND
:或者分别比较两列和链掩码&
按位AND
:
df = df.loc[df['min'].gt(0) & df['max'].gt(0), ['Open','min','max','Loc']].set_index('Loc')
EDIT:编辑:
Because error:因为错误:
''>' not supported between instances of 'str' and 'int', ''>' 在 'str' 和 'int' 的实例之间不支持,
it means there are string repr of values in min
or max
columns (or both), so convert values to numbers before solutions above:这意味着在min
或max
列(或两者)中有值的字符串 repr,因此在上述解决方案之前将值转换为数字:
df['min'] = pd.to_numeric(df['min'], errors='coerce')
df['max'] = pd.to_numeric(df['max'], errors='coerce')
Building your example dataframe:构建您的示例 dataframe:
df = pd.DataFrame(
data={
"Date": ["2020-06-15 14:00:00", "2020-06-15 18:00:00", "2020-06-15 22:00:00"],
"Open": [0.000123, 0.000128, 0.000125],
"High": [0.000130, 0.000129, 0.000126],
"Low": [0.000121, 0.000123, 0.000122],
"Close": [0.000128, 0.000125, 0.000123],
"Volume": [1467828.0, 1264642.0, 723738.0],
"min": [0.00012081, 0, 0],
"max": [0.00013040, 0, 0],
"Loc": [0, 1, 2],
}
)
df.set_index("Date", drop=False, inplace=True)
A solution would be this:一个解决方案是这样的:
# Set the index to a different column
# ("df2" is a copy of "df")
df2 = df.set_index("Loc")
# Keep only some columns
df2 = df2[["Open", "min", "max"]]
# Filter rows based on a condition
df2 = df2[(df2["min"] > 0) & (df2["max"] > 0)]
df2
would be like this: df2
会是这样的:
Open min max
Loc
0 0.000123 0.000121 0.00013
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.