草庐IT

dataframe

全部标签

python - Pandas DataFrame 到列表列表

将列表列表转换为pandas数据框很容易:importpandasaspddf=pd.DataFrame([[1,2,3],[3,4,5]])但是如何将df转回列表列表?lol=df.what_to_do_now?printlol#[[1,2,3],[3,4,5]] 最佳答案 您可以访问底层数组并调用它的tolist方法:>>>df=pd.DataFrame([[1,2,3],[3,4,5]])>>>lol=df.values.tolist()>>>lol[[1L,2L,3L],[3L,4L,5L]]

python - Pandas DataFrame 到列表列表

将列表列表转换为pandas数据框很容易:importpandasaspddf=pd.DataFrame([[1,2,3],[3,4,5]])但是如何将df转回列表列表?lol=df.what_to_do_now?printlol#[[1,2,3],[3,4,5]] 最佳答案 您可以访问底层数组并调用它的tolist方法:>>>df=pd.DataFrame([[1,2,3],[3,4,5]])>>>lol=df.values.tolist()>>>lol[[1L,2L,3L],[3L,4L,5L]]

python - 将 Pandas DataFrame 的行转换为列标题,

我必须处理的数据有点乱。它的数据中有标题名称。如何从现有的pandas数据框中选择一行并将其(重命名为)列标题?我想做这样的事情:header=df[df['old_header_name1']=='new_header_name1']df.columns=header 最佳答案 In[21]:df=pd.DataFrame([(1,2,3),('foo','bar','baz'),(4,5,6)])In[22]:dfOut[22]:01201231foobarbaz2456将列标签设置为等于第2行(索引位置1)中的值:In[23]

python - 将 Pandas DataFrame 的行转换为列标题,

我必须处理的数据有点乱。它的数据中有标题名称。如何从现有的pandas数据框中选择一行并将其(重命名为)列标题?我想做这样的事情:header=df[df['old_header_name1']=='new_header_name1']df.columns=header 最佳答案 In[21]:df=pd.DataFrame([(1,2,3),('foo','bar','baz'),(4,5,6)])In[22]:dfOut[22]:01201231foobarbaz2456将列标签设置为等于第2行(索引位置1)中的值:In[23]

python - 将列表或系列作为一行 append 到 pandas DataFrame?

所以我已经初始化了一个空的pandasDataFrame,我想在这个DataFrame中迭代地追加列表(或系列)作为行。这样做的最佳方法是什么? 最佳答案 有时在pandas之外完成所有append操作会更容易,然后一次性创建DataFrame。>>>importpandasaspd>>>simple_list=[['a','b']]>>>simple_list.append(['e','f'])>>>df=pd.DataFrame(simple_list,columns=['col1','col2'])col1col20ab1ef

python - 将列表或系列作为一行 append 到 pandas DataFrame?

所以我已经初始化了一个空的pandasDataFrame,我想在这个DataFrame中迭代地追加列表(或系列)作为行。这样做的最佳方法是什么? 最佳答案 有时在pandas之外完成所有append操作会更容易,然后一次性创建DataFrame。>>>importpandasaspd>>>simple_list=[['a','b']]>>>simple_list.append(['e','f'])>>>df=pd.DataFrame(simple_list,columns=['col1','col2'])col1col20ab1ef

python - 如何将数据框转换为字典

我有一个包含两列的数据框,并打算将其转换为字典。第一列是键,第二列是值。数据框:idvalue0010.2115.7227.4我该怎么做? 最佳答案 如果lakes是你的DataFrame,你可以这样做area_dict=dict(zip(lakes.id,lakes.value)) 关于python-如何将数据框转换为字典,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1869

python - 如何将数据框转换为字典

我有一个包含两列的数据框,并打算将其转换为字典。第一列是键,第二列是值。数据框:idvalue0010.2115.7227.4我该怎么做? 最佳答案 如果lakes是你的DataFrame,你可以这样做area_dict=dict(zip(lakes.id,lakes.value)) 关于python-如何将数据框转换为字典,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1869

python - 将 pandas 数据框中的列从 int 转换为 string

我在pandas中有一个混合了int和str数据列的数据框。我想首先连接数据框中的列。为此,我必须将int列转换为str。我尝试过如下操作:mtrx['X.3']=mtrx.to_string(columns=['X.3'])或mtrx['X.3']=mtrx['X.3'].astype(str)但在这两种情况下它都不起作用,我收到一条错误消息,提示“无法连接'str'和'int'对象”。连接两个str列效果很好。 最佳答案 In[16]:df=DataFrame(np.arange(10).reshape(5,2),columns

python - 将 pandas 数据框中的列从 int 转换为 string

我在pandas中有一个混合了int和str数据列的数据框。我想首先连接数据框中的列。为此,我必须将int列转换为str。我尝试过如下操作:mtrx['X.3']=mtrx.to_string(columns=['X.3'])或mtrx['X.3']=mtrx['X.3'].astype(str)但在这两种情况下它都不起作用,我收到一条错误消息,提示“无法连接'str'和'int'对象”。连接两个str列效果很好。 最佳答案 In[16]:df=DataFrame(np.arange(10).reshape(5,2),columns