我想在python中创建一个矩阵,它使用的是每个状态转换组合的所有频率。例如,如果我们有3个状态(a,b,c)和两个时间段(1 & 2),那么我可以使用以下列进行数据处理:
# Import pandas library
import pandas as pd
# initialize list of lists
data = [['a to a', 20],['a to b', 10], ['a to c', 5],
['b to a', 7],['b to b', 30],['
我有一个带有零的dataframe Exposure,构造如下:
Exposure = pd.DataFrame(0, index=dates, columns=tickers)
和一个带有数据的DataFrame df。
我想填一些从df到Exposure的数据
for index, column in df.iterrows():
# value of df(index,column) to be filled at Exposure(index,column)
如何将at (index,column) of Exposure的值与df(index,column)的
我正在使用python pandas进行数据分析,并且我想更改dataframe中序列的名称。
这是可行的,但似乎效率很低:
AA = pandas.DataFrame( A )
for series in A:
AA[A_prefix+series] = A[series]
del A[series]
有没有办法就地更改系列名称?
我使用输入的SQL查询在python上构建了一个数据框架。在此之后,我命名我的列,并确保使用NaN值隔离列是很好的:
cursor.execute(raw_input("Enter your SQL query: "))
records = cursor.fetchall()
import pandas as pd
dframesql = pd.DataFrame(records)
dframesql.columns = [i[0] for i in cursor.description]
当我想要将包含数据的行数与数据框中的总行数进行比较时,问题出现了:
dframeline
我是Python和StackOverflow的新手。希望我能正确地发布:)
问题:在每个for循环中,我都试图创建一个新的dataframe,向其中添加列,并将数据填充到列中。
方法:通过阅读以前的文章,我了解到我可以使用“”创建新的数据格式,但这不是最佳实践。或者,我可以创建一个dic,然后修改df。也一直试图使用嵌入式for循环
问题:实际上修改和填充创建的(和空的) df
代码:
df1 = an existing dataframe with date as its index and profit, tax, and revenue for columns
#tablelist w
如何在python dataframe中随机选择和赋值给给定的行数。列B只包含1和0。假设我有一个数据帧,如下 Col A Col B
A 0
B 0
A 0
B 0
C 0
A 0
B 0
C 0
D 0
A 0 我的目标是随机选择5%的行,并将列B的值更改为1。我看到了df.sample(),但这不允许我就地更改列数据
我正在使用pandas DataFrame创建一个嵌套字典,其中包含一个包含名称的列。每个字典元素都是一个嵌套字典,每个嵌套字典的键都是DataFrame列中的一个名称。
我使用下面这一行来填充字典:
for row, name in map_datafile.iterrows():
material_count[name._get_value(label='NAME')] = {}
由于某些原因,生成的字典仅包含DataFrame中包含的586个名称中的579个。出于这个原因,当我使用从这个字典中计算出的新数据添加一个列时,我得到了这个错误:
Val
我的代码如下所示,我使用pd.DataFrame.from_records将数据填充到dataframe中,但要处理请求并将数据从包含22百万行的sql表加载到df中需要花一定的时间:1 H40min 30。
# I skipped some of the code, since there are no problems with the extract of the query, it's fast
cur = con.cursor()
def db_select(query): # takes the request text and sends it to the data_