我有包含200+列的pandas数据帧。所有列都是int类型。我需要将它们转换为浮点型。我找不到做这件事的方法。我试过了
for column in X_data:
X_data[column].astype('float64')
但是在for循环之后,当我打印X_data.dtypes时,所有的列都只显示为int。我也尝试过X_data = X_data.apply(pd.to_numeric),但它没有转换为float。
数据帧是从csv文件加载中构建的。
我将csv文件读入pandas数据帧,并希望将具有二进制答案的列从yes/no字符串转换为1/0整数。下面,我展示了其中一列("sampleDF“是pandas数据帧)。
In [13]: sampleDF.housing[0:10]
Out[13]:
0 no
1 no
2 yes
3 no
4 no
5 no
6 no
7 no
8 yes
9 yes
Name: housing, dtype: object
非常感谢您的帮助!
我刚接触pandas,我正在尝试更新基于'Id'列的'Text‘列。
我的数据是这样的[(1,'One'), (0, 'Zero'), (4,'Four'), (3, 'Three')]我在数据中也有一些丢失的id,对于那些丢失的id,我必须将其留空
原始数据帧
Id A Text
0 0 NaN
1 1 NaN
2 2 NaN
3 3 NaN
4 4 NaN
最终数据帧
Id A Text
0 0 Zer
我正在学习Python和Pandas,并试图找出最有效的方法来比较两个数据帧上的多个选定列,以找到匹配的列。例如,如果我有以下两个数据帧: Frame 1
A B C D E F
001 10 0 0 10 0 10
Frame 2
A B C D E F
200 10 0 10 0 10 0
201 0 10 10 0 0 10
202 0 10 0 0 0 0
2
Python 3.8,使用Pandas。 我正在尝试去掉pandas数据框列' data‘中负数的符号,只留下幅度,即所有值都是正数。本质上,将一列中的值乘以-1,但仅当它为负值时。这是比较容易的一点。然后在数据帧中创建一条它已被颠倒的记录。因此在本例中创建另一个名为"Tubes Inverted“列 #Check sign and create a column recording if this has been inverted.
num = df['DATA']._get_numeric_data()
我正在尝试从pandas数据帧中的单个列中的字符串中找到一个最大的数字,然后根据最大结果创建另一个列。 我的数据帧: number_quotes
0 I have 1-50 ice-creams
1 4 people out of 10 said hello
2 8889 or 9500 but could be 10903 期望的结果: number_quotes max_number
0 I have 1-50 ice-creams 50
1 4 people out of 10 said hello
我正在尝试计算数据帧中每一列的最大和最小长度,该数据帧中有一些缺失值。Pandas将这些缺失值视为"NaN“,并将长度计为3。如何在计算最大和最小长度时完全忽略缺失值?下面是我的代码: import pandas as pd
columnname=[]
maxColumnLenghts = []
minColumnLenghts=[]
for colname in df.columns:
columnname.append(colname)
for col in range(len(df.columns))
我有一个函数,它接收数据帧并将其作为表写入SQL。 def insert(df):
with connection.cursor as cur:
cur.execute('''create tablaexyz.xyz
(ID integer,
first_name varchar(100),
last_name varchar(100))''')
d
我想找出pandas数据帧中的哪些列具有不连续的数据。所谓“不连续”,我的意思是这些值从某个值变为零,然后再有一些值。
[0,0,0,1,2,3,4,5,0,0,0] # continuous
[0,0,0,1,2,0,4,5,0,0,0] # not continuous
我已经设法实现了一些可以做到这一点的代码,使用for循环来迭代数据帧的每一列。我在下面制作了一个工作代码片段来说明我的意思:
import numpy as np
import pandas as pd
def find_discontinuous(series):
switch = 0
for inde