我有一个熊猫数据框,如下所示:
Names Cider Juice Subtotal (Cider) Subtotal (Juice) Total
Richard 13 9 $ 71.5 $ 40.5 $ 112.0
George 7 21 $ 38.5 $ 94.5 $ 133.0
Paul 0 23 $ 0.0 $ 103.5 $ 103.5
John 22 5 $ 121.0 $ 22.5 $ 143.5
Total 42 58 $ 231.0 $ 261.0 $ 492.0
Average 10.5 14.5 $ 57.75 $ 65.25 $ 123.0
Run Code Online (Sandbox Code Playgroud)
我希望所有浮点数均为'.2f'(2位浮点数)数字。.applymap()
不起作用,因为我在“名称”列中输入了字符串类型。是否有围绕使用的解决方法,.applymap()
或者有更好的方法来做到这一点?
import pandas as pd
df = pd.DataFrame(columns=["Names", "Cider", "Juice", "Subtotal(Cider)", "Subtotal(Juice)", "Total"]) …
Run Code Online (Sandbox Code Playgroud) 我创建了一个pandas数据帧
df = pd.DataFrame(data=[[1],[2],[3],[1],[2],[3],[1],[2],[3]])
df
Out[19]:
0
0 1
1 2
2 3
3 1
4 2
5 3
6 1
7 2
8 3
Run Code Online (Sandbox Code Playgroud)
我计算长度= 3的窗口的75%百分位数
df.rolling(window=3,center=False).quantile(0.75)
Out[20]:
0
0 NaN
1 NaN
2 2.0
3 2.0
4 2.0
5 2.0
6 2.0
7 2.0
8 2.0
Run Code Online (Sandbox Code Playgroud)
然后检查我分别在第一个窗口计算75%
df.iloc[0:3].quantile(0.75)
Out[22]:
0 2.5
Name: 0.75, dtype: float64
Run Code Online (Sandbox Code Playgroud)
为什么我得到不同的价值?
我正在处理一个包含3个值'event1','event2'和'event3'的列事件的数据帧.我正在寻找一种方法来选择具有特定顺序事件的行['event1','event2','event3'].
我试过了:
df[df['Event'].isin(['event1', 'event2', 'event3'])]
Run Code Online (Sandbox Code Playgroud)
但结果是整个数据帧.
import pandas as pd
df = pd.DataFrame([['event1','01:22:52.134'],['event2','03:21:31.123'], ['event1','21:12:52.544'],['event3','23:12:31.216'],['event1','10:22:02.134'],['event2','06:52:48.184'], ['event3','12:52:46.188'], ['event3','06:52:46.184'], ['event1','13:33:46.235'], ['event2','14:35:12.235'], ['event3','14:59:12.177']], columns=["Events",'Time'])
df
Run Code Online (Sandbox Code Playgroud) 我的数据框只有1列.我想将所有'0'替换为np.nan但我无法实现.
数据帧称为区域.我试过了:
area.replace(0,np.nan)
area.replace(to_replace=0,np.nan)
area.replace(to_replace=0,value=np.nan)
area.replace('0',np.nan)
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
我有一个rdd:
a,1,2,3,4
b,4,6
c,8,9,10,11
Run Code Online (Sandbox Code Playgroud)
我想将其转换为具有索引的Spark数据框:
df:
Index Name Number
0 a 1,2,3,4
1 b 4,6
2 c 8,9,10,11
Run Code Online (Sandbox Code Playgroud)
我尝试拆分RDD:
parts = rdd.flatMap(lambda x: x.split(","))
Run Code Online (Sandbox Code Playgroud)
但结果是:
a,
1,
2,
3,...
Run Code Online (Sandbox Code Playgroud)
如何将RDD拆分并转换为pyspark中的Dataframe,使第一个元素作为第一列,其余元素合并为一列?
如解决方案中所述:
rd = rd1.map(lambda x: x.split("," , 1) ).zipWithIndex()
rd.take(3)
Run Code Online (Sandbox Code Playgroud)
输出:
[(['a', '1,2,3,4'], 0),
(['b', '4,6'], 1),
(['c', '8,9,10,11'], 2)]
Run Code Online (Sandbox Code Playgroud)
下一步:
rd2=rd2=rd.map(lambda x,y: (y, x[0] , x[1]) ).toDF(["index", "name" ,"number"])
rd2.collect()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
An error occurred while calling
z:org.apache.spark.api.python.PythonRDD.runJob.
: org.apache.spark.SparkException: Job aborted due to stage failure:
Task 0 in stage …
Run Code Online (Sandbox Code Playgroud) 我试图找到仅包含空字符串的列中的单元格数量''
。的df
样子:
currency
USD
EUR
ILS
HKD
Run Code Online (Sandbox Code Playgroud)
代码是:
df['currency'].str.contains(r'\s*')
Run Code Online (Sandbox Code Playgroud)
但是代码也将具有实际字符串值的单元格识别为包含空字符串。
我想知道如何解决该问题,因为它只能检测仅包含空字符串的单元格。
更新使用整个站点中发现的不同解决方案:
我仍然无法使用stack和ldply函数获得所需的输出:
所需的输出如下所示:
Dataset Samples
1 WGS nrow(WGS.ped)
2 WES nrow(WES.ped.exp)
3 MIPS nrow(MIPS.ped.exp)
Run Code Online (Sandbox Code Playgroud)
1)ldply
:如何给列V1
和列分配名称.id
?
ldply(list(WGS=WGS.ped, WES=WES.ped.exp, MIPS=mips.ped.exp),
function(l)(Samples=nrow(l)))
.id V1
1 WGS 3908
2 WES 26367
3 MIPS 14193
Run Code Online (Sandbox Code Playgroud)
2)ldply
:如何给列V1
和列分配名称.id
?
ldply(list(WGS=WGS.ped, WES=WES.ped.exp, MIPS=mips.ped.exp), nrow)
.id V1
1 WGS 3908
2 WES 26367
3 MIPS 14193
Run Code Online (Sandbox Code Playgroud)
3)lapply %>% as.data.frame
:将数据帧名称作为列而不是第一列' Dataset
'返回。
lapply(list(WGS=WGS.ped, WES=WES.ped.exp, MIPS=mips.ped.exp), nrow) %>%
as.data.frame
WGS WES MIPS
1 …
Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的数据框:
Name A B C
D1 1 3 3
D2 2 4 4
D3 2 1 1
Run Code Online (Sandbox Code Playgroud)
如何创建一个相同大小的新数据框,其中每个值都是今天的日期减去我的第一个数据帧的值?
例如,如果今天是2018-04-27,我的新数据框将如下所示:
Name A B C
D1 2018-04-26 2018-04-24 2018-04-24
D2 2018-04-25 2018-04-23 2018-04-23
D3 2018-04-25 2018-04-26 2018-04-26
Run Code Online (Sandbox Code Playgroud)
我认为解决方案将包括类似的东西
df2.iloc[1,1] = datetime.today() - timedelta(days=df1[1,1])
Run Code Online (Sandbox Code Playgroud)
但我遇到了各种类型的错误和循环原始df的问题
我试图根据逗号/空格分隔将列拆分为多个列.
我的数据框目前看起来像
Item Colors
0 ID-1 Red, Blue, Green
1 ID-2 Red, Blue
2 ID-3 Blue, Green
3 ID-4 Blue
4 ID-5 Red
Run Code Online (Sandbox Code Playgroud)
我想将"颜色"列转换为红色,蓝色和绿色,如下所示:
Item Red Blue Green
0 ID-1 1 1 1
1 ID-2 1 1 0
2 ID-3 0 1 1
3 ID-4 0 1 0
4 ID-5 1 0 1
Run Code Online (Sandbox Code Playgroud)
我真的不知道该怎么做.任何帮助将不胜感激.
我有一个包含整数的数据框,但是当我旋转它时,它会创建浮点数,因此我无法弄清原因:
我的数据框(dfDis)如下所示:
Year Type Total
0 2006 A talk or presentation 34
1 2006 A magazine, newsletter or online publication 33
2 2006 A formal working group, expert panel or dialogue 2
3 2006 Scientific meeting (conference/symposium etc.) 10
4 2006 A press release, press conference or response ... 6
....
Run Code Online (Sandbox Code Playgroud)
我的枢纽代码是:
dfDisB = pd.pivot_table(dfDis, index=['Year'], columns = ['Type'],fill_value=0)
Run Code Online (Sandbox Code Playgroud)
出于某种原因,dfDisB最终会像这样(很抱歉格式化,希望您能理解):
Total
Type A broadcast e.g. TV/radio/film/podcast (other than news/press) A formal working group, expert panel or dialogue A magazine, newsletter …
Run Code Online (Sandbox Code Playgroud) dataframe ×10
python ×9
pandas ×8
apache-spark ×1
data-science ×1
dplyr ×1
lapply ×1
percentile ×1
pivot-table ×1
pyspark ×1
r ×1
rdd ×1
series ×1
stack ×1
string ×1