![](/img/trans.png)
[英]Convert string to json where some values are wrapped in square brackets
[英]Dropping Rows that Contain a Specific String wrapped in square brackets?
我正在嘗試刪除包含包含在列中的字符串的行。 我想刪除所有包含字符串 '[removed]'、'[deleted]' 的值。 我的 df 看起來像這樣:
Comments
1 The main thing is the price appreciation of the token (this determines the gains or losses more
than anything). Followed by the ecosystem for the liquid staking asset, the more opportunities
and protocols that accept the asset as collateral, the better. Finally, the yield for staking
comes into play.
2 [deleted]
3 [removed]
4 I could be totally wrong, but sounds like destroying an asset and claiming a loss, which I
believe is fraudulent. Like someone else said, get a tax guy - for this year anyway and then
you'll know for sure. Peace of mind has value too.
我試過df[df["Comments"].str.contains("removed")==False]
但是當我嘗試保存 dataframe 時,它仍然沒有被刪除。
編輯:我的完整代碼
import pandas as pd
sol2020 = pd.read_csv("Solana_2020_Comments_Time_Adjusted.csv")
sol2021 = pd.read_csv("Solana_2021_Comments_Time_Adjusted.csv")
df = pd.concat([sol2021, sol2020], ignore_index=True, sort=False)
df[df["Comments"].str.contains("deleted")==False]
df[df["Comments"].str.contains("removed")==False]
嘗試這個
我已經為評論列創建了一個數據框並使用了我自己的評論,但它應該適合你
import pandas as pd
sample_data = { 'Comments': ['first comment whatever','[deleted]','[removed]','last comments whatever']}
df = pd.DataFrame(sample_data)
data = df[df["Comments"].str.contains("deleted|removed")==False]
print(data)
output 我得到了
Comments
0 first comment whatever
3 last comments whatever
你可以這樣做:
new_df = df[~(df['Comments'].str.startswith('[') & df['Comments'].str.endswith(']'))].reset_index(drop=True)
Output:
>>> new_df
Comments
0 The main thing is the price appreciation of th...
3 I could be totally wrong, but sounds like dest...
這將刪除該行的Comments
列的值以[
開頭並以]
結尾的所有行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.