我有一个具有以下格式的文本文件:
1: frack 0.733, shale 0.700,
10: space 0.645, station 0.327, nasa 0.258,
4: celebr 0.262, bahar 0.345
Run Code Online (Sandbox Code Playgroud)
我需要将此文本转换为以下格式的DataFrame:
Id Term weight
1 frack 0.733
1 shale 0.700
10 space 0.645
10 station 0.327
10 nasa 0.258
4 celebr 0.262
4 bahar 0.345
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
这是我的数据框
index duration
1 7 year
2 2day
3 4 week
4 8 month
Run Code Online (Sandbox Code Playgroud)
我需要将数字与时间分开并将它们放在两个新列中。输出是这样的:
index duration number time
1 7 year 7 year
2 2day 2 day
3 4 week 4 week
4 8 month 8 month
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
df ['numer'] = df.duration.replace(r'\d.*' , r'\d', regex=True, inplace = True)
df [ 'time']= df.duration.replace (r'\.w.+',r'\w.+', regex=True, inplace = True )
Run Code Online (Sandbox Code Playgroud)
但它不起作用。有什么建议吗?
我还需要根据时间列的值创建另一列。所以新的数据集是这样的:
index duration number time time_days
1 7 year 7 year 365
2 2day 2 day 1
3 4 week …Run Code Online (Sandbox Code Playgroud) 下面是两个数据框: 数据框A:
index codes
1 A
2 B
3 C
4 D
Run Code Online (Sandbox Code Playgroud)
数据框 B
index cym
1 A
2 L
3 F
4 B
5 N
6 X
Run Code Online (Sandbox Code Playgroud)
A 和 B 的长度不相等。我想比较列“代码”(数据框 A)与列“cym”(数据框 B)并返回这两列之间的差异加上数据框 B 索引列中的数据。输出是这样的:
index cym
2 L
3 F
5 N
6 X
Run Code Online (Sandbox Code Playgroud)
我尝试使用 merge 和 equals 函数来解决它。但我无法生成输出。
以下是数据框的子集:
id words A B C D E
1 new 1 1
2 good 1
3 star 1
4 never
5 final
Run Code Online (Sandbox Code Playgroud)
我想将一个新变量(称为FF)定义为一个新列,并为其分配1,如果所有其他变量(列)的值都为"null".新数据框将如下所示:
id words A B C D E FF
1 new 1 1
2 good 1
3 star 1
4 never 1
5 final 1
Run Code Online (Sandbox Code Playgroud)
我怎么能用python和Pandas做到这一点?谢谢.
我有以下数据集.
我有以下数据集.如您所见,A,B,D和R列中的某些值缺失.我将使用前一行中的精确值替换值.例如,在第4行中,缺少B列和R列的值,我需要用前一列中的21和2替换它们.
A B D R sentence ADR1 ADR2
135 21 EffexorXR.21 1 lack of good feeling. good feeling
0 0
136 21 EffexorXR.21 2 Feel disconnected disconnected feel
136 EffexorXR.21 0 0
142 22 EffexorXR.22 1 Weight gain gain
142 1 1
Run Code Online (Sandbox Code Playgroud)
最终的出局是这样的:
A B D R sentence ADR1 ADR2
135 21 EffexorXR.21 1 lack of good feeling. good feeling
135 21 EffexorXR.21 1 1 0
136 21 EffexorXR.21 2 Feel disconnected disconnected feel
136 21 EffexorXR.21 2 0 …Run Code Online (Sandbox Code Playgroud) 对于这部分html代码:
html3= """<a name="definition"> </a>
<h2><span class="sectioncount">3.342.2323</span> Content Logical Definition <a title="link to here" class="self-link" href="valueset-investigation"><img src="ta.png"/></a></h2>
<hr/>
<div><p from the following </p><ul><li>Include these codes as defined in http://snomed.info/sct<table><tr><td><b>Code</b></td><td><b>Display</b></td></tr><tr><td>34353553</td><td>Examination / signs</td><td/></tr><tr><td>35453453453</td><td>History/symptoms</td><td/></tr></table></li></ul></div>
<p> </p>"""
Run Code Online (Sandbox Code Playgroud)
我将使用 beautifulsoup 来查找 h2 ,其文本等于“内容逻辑定义”和下一个兄弟姐妹。但是beautifulsoup找不到h2。以下是我的代码:
soup = BeautifulSoup(html3, "lxml")
f= soup.find("h2", text = "Content Logical Definition").nextsibilings
Run Code Online (Sandbox Code Playgroud)
这是一个错误:
AttributeError: 'NoneType' object has no attribute 'nextsibilings'
Run Code Online (Sandbox Code Playgroud)
文本中有几个“h2”,但唯一使这个h2独特的字符是“内容逻辑定义”。找到这个 h2 后,我将从表中提取数据并在其下方列出。
这是数据框的子集:
index id drug sentences SS1 SS2
1 2 lex very bad 0 1
2 3 gym very nice 1 1
3 7 effex hard 1 0
4 8 cymba poor 1 1
Run Code Online (Sandbox Code Playgroud)
我想找到SS1和SS2不同的行,然后根据它创建一个新的数据帧.输出应该是这样的:
index id drug sentences SS1 SS2
1 2 lex very bad 0 1
3 7 effex hard 1 0
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
df [['index','id', 'drug', 'sentences', 'SS1', 'SS2' ]] = np.where(df.SS1 != df.SS2)
Run Code Online (Sandbox Code Playgroud)
但它有以下错误: ValueError: Must have equal len keys and value when setting with an ndarray
有什么建议吗?
我正在尝试使用以下数据集创建字典:
id value
1 a
1 b
1 c
2 e
2 f
2 g
3 h
3 g
3 l
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的
{1: [a,b,c], 2:[e, f, g], 3: [h, g, l]}
Run Code Online (Sandbox Code Playgroud)
我知道有关如何创建字典的一些参考,但它们都没有提供这样的输出.
谢谢.
我有一个这样的列表:
a = [c0001203, c0334, c0000456, c034554, c00034506].我需要在'c'之后删除'c'和全0,直到数据以数字开头.每个数据的长度是可变的.输出应该是这样的:
a = [1203, 334, 456, 34506].如何在不使用正则表达式的情况下完成?
谢谢.
我知道如何使用带有填充或填充的groupby方法来估算缺少的值。但是我的问题是,我需要首先在“日期”列中找到与“得分”列中的空值最接近的日期,如果分数列中的值不为空,则用该值进行插补。如果该值为空,则需要搜索另一个最近的日期。我可以遍历行并执行此操作,但是速度非常慢。
这是数据的示例:
df = pd.DataFrame(
{'cn': [1, 1, 1, 1, 2, 2, 2],
'date': ['01/10/2017', '02/09/2016', '02/10/2016','01/20/2017', '05/15/2019', '02/10/2016', '02/10/2017'],
'score': [np.nan, np.nan, 6, 5, 4, np.nan, 8]})
cn date score
0 1 01/10/2017 NaN
1 1 02/09/2016 NaN
2 1 02/10/2016 6
3 1 01/20/2017 5
4 2 05/15/2019 4
5 2 02/10/2016 NaN
6 2 02/10/2017 8.0
Run Code Online (Sandbox Code Playgroud)
输出应该是
cn date score
0 1 01/10/2017 5
1 1 02/09/2016 6
2 1 02/10/2016 6
3 1 01/20/2017 5
4 …Run Code Online (Sandbox Code Playgroud) python ×10
pandas ×8
dataframe ×2
compare ×1
dictionary ×1
html ×1
html-parsing ×1
missing-data ×1
regex ×1