我有这段代码可以从Type列中删除空值,尤其是Dog。
cd.loc[cd['Type'] == 'Dog'].dropna(subset = ['Killed'], inplace = True)
Run Code Online (Sandbox Code Playgroud)
我想当与Type = Dog关联的['Killed']列具有NaN值时使用dropna。
上面的代码生成此熊猫错误:
A value is trying to be set on a copy of a slice from a DataFrame
Run Code Online (Sandbox Code Playgroud)
当['Type'] =='Dog'时,还有其他方法可以在哪里放置['Killed']吗?
(这是我的第一篇文章),对不起,如果我无法正确解释
我有一个包含名称、颜色、重量、大小、种子的水果数据集
Fruit dataset
Name Colour Weight Size Seeds Unnamed
Apple Apple Red 10.0 Big Yes
Apple Apple Red 5.0 Small Yes
Pear Pear Green 11.0 Big Yes
Banana Banana Yellow 4.0 Small Yes
Orange Orange Orange 5.0 Small Yes
Run Code Online (Sandbox Code Playgroud)
问题在于,颜色列是名称的重复列,并且值向右移动 1 列,从而创建了一个无用的列(未命名),其中包含属于列种子的值。是否有一种简单的方法可以删除 Color 中的重复值并将其余的列值从 weight 开始向左移回 1 列。我希望我不会在这里混淆任何人。
想要的结果
Fruit dataset
Name Colour Weight Size Seeds Unnamed(will be dropped)
Apple Red 10.0 Big Yes
Apple Red 5.0 Small Yes
Pear Green 11.0 Big Yes
Banana Yellow 4.0 Small Yes
Orange …Run Code Online (Sandbox Code Playgroud) 我试图使用具有相似列值的行来估算值.
例如,我有这个数据帧
one | two | three
1 1 10
1 1 nan
1 1 nan
1 2 nan
1 2 20
1 2 nan
1 3 nan
1 3 nan
Run Code Online (Sandbox Code Playgroud)
我想使用列['one']和['two']的键,这是相似的,如果列['three']不完全是nan,那么从列中的值为一行类似键的现有值'3']
这是我的愿望结果
one | two | three
1 1 10
1 1 10
1 1 10
1 2 20
1 2 20
1 2 20
1 3 nan
1 3 nan
Run Code Online (Sandbox Code Playgroud)
您可以看到键1和3不包含任何值,因为现有值不存在.
我尝试过使用groupby fillna()
df['three'] = df.groupby(['one','two'])['three'].fillna()
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误.
我尝试了向前填充,这给了我相当奇怪的结果,它向前填充第2列.我正在使用此代码进行前向填充.
df['three'] = df.groupby(['one','two'], sort=False)['three'].ffill()
Run Code Online (Sandbox Code Playgroud)
感谢您的时间.
浏览到rest_framework api页面时遇到无法解决的错误。
完整错误(Django错误)为:
Got AttributeError when attempting to get a value for field `process` on
serializer `ResultSerializer`.
The serializer field might be named incorrectly and not match any attribute
or key on the `Shop` instance.
Original exception text was: 'Shop' object has no attribute 'process'.
Run Code Online (Sandbox Code Playgroud)
似乎序列化程序试图在另一个名为ResultSerializer的序列化程序中给定的字段中获取值,但找不到它。我检查了所有字段,它们都是正确的。
这是我的models.py
from django.db import models
class ResultSet(models.Model):
process = models.CharField(max_length=10)
subprocess = models.CharField(max_length=10)
class Shop(models.Model):
Establishment = models.CharField(max_length=100)
Address = models.CharField(max_length=100)
Suburb = models.CharField(max_length=50)
Postcode = models.CharField(max_length=10)
State = models.CharField(max_length=5)
Establishment_Type = models.CharField(max_length=20)
latitude …Run Code Online (Sandbox Code Playgroud) 我有一个关于根据其他列的总和创建pandas数据帧的问题.
例如,我有这个数据帧
Country | Accident
England Car
England Car
England Car
USA Car
USA Bike
USA Plane
Germany Car
Thailand Plane
Run Code Online (Sandbox Code Playgroud)
我想根据国家/地区的所有事故的总和值制作另一个数据框.我们将忽略事故的类型,同时根据国家总结事故.
我的愿望数据框架看起来像这样
Country | Sum of Accidents
England 3
USA 3
Germany 1
Thailand 1
Run Code Online (Sandbox Code Playgroud) 我知道问题名称有点含糊不清.
我的目标是在我的数据框中根据2列+唯一值分配全局键列.
例如
CountryCode | Accident
AFG Car
AFG Bike
AFG Car
AFG Plane
USA Car
USA Bike
UK Car
Run Code Online (Sandbox Code Playgroud)
设Car = 01,Bike = 02,Plane = 03
我的愿望全局密钥格式是[事故] [CountryCode] [UniqueValue]
唯一值是类似[Accident] [CountryCode]的计数
因此,如果Accident = Car和CountryCode = AFG并且它是第一次出现,则全局密钥将为01AFG01
所需的数据框如下所示:
CountryCode | Accident | GlobalKey
AFG Car 01AFG01
AFG Bike 02AFG01
AFG Car 01AFG02
AFG Plane 01AFG03
USA Car 01USA01
USA Bike 01USA02
UK Car 01UK01
Run Code Online (Sandbox Code Playgroud)
我尝试运行for循环将Accident Number和CountryCode一起添加
例如:
globalKey = []
for x in range(0,6):
string = df.iloc[x, 1]
string2 = …Run Code Online (Sandbox Code Playgroud) 我有一个字符串,其中包含汽车的注册号码,例如
1FX9JE - 2012 Audi A3 Ambition Sportback MY12 Stronic
Run Code Online (Sandbox Code Playgroud)
我想匹配除注册号码之外的所有内容,以及破折号之后的任何内容。
我想出的正则表达式是(php)
\s.[^-]*$
Run Code Online (Sandbox Code Playgroud)
我提出的初始正则表达式仅当字符串仅包含 1 个破折号时才可以匹配破折号后的任何内容。例如https://regex101.com/r/Jao8W0/1
但是,如果字符串有超过 1 个破折号。正则表达式不可用。例如: https: //regex101.com/r/Jao8W0/2
无论如何,我是否可以匹配第一个破折号之后的任何内容,即使字符串在第一个破折号之后包含额外的破折号。
谢谢