简体   繁体   English

如何在熊猫数据框中一次将一个列表添加到一个单元格

[英]How to add one list to one cell at a time in a pandas dataframe

I have a for loop which prints a list of hex strings for each iteration我有一个 for 循环,它为每次迭代打印一个十六进制字符串列表

for i in range(0, 10): print([str(hex(i))[::2], str(hex(i*10))[::2], str(hex(i+10))[::2]])

the output is输出是

['00', '00', '0a'] ['01', '0a', '0b'] ['02', '01', '0c'] ['03', '01', '0d'] ['04', '02', '0e'] ['05', '03', '0f'] ['06', '03', '01'] ['07', '04', '01'] ['08', '05', '01'] ['09', '05', '01']

I want to read the lists into one cell at a time such that the dataframe should look like this我想一次将列表读入一个单元格,这样数据框应该看起来像这样

idx身份证 NEWcoL NEWCOL
1 1个 ['00', '00', '0a'] ['00', '00', '0a']
2 2个 ['01', '0a', '0b'] ['01', '0a', '0b']
3 3个 ['02', '01', '0c'] ['02', '01', '0c']
4 4个 ['03', '01', '0d'] ['03', '01', '0d']
5 5个 ['04', '02', '0e'] ['04', '02', '0e']
6 6个 ['05', '03', '0f'] ['05', '03', '0f']
7 7 ['06', '03', '01'] ['06', '03', '01']
8 8个 ['07', '04', '01'] ['07', '04', '01']
9 9 ['08', '05', '01'] ['08', '05', '01']
10 10 ['09', '05', '01'] ['09', '05', '01']

Can you try this:你能试试这个吗:

my_list=[]
for i in range(0, 10):
    my_list.append([str(hex(i))[::2], str(hex(i*10))[::2], str(hex(i+10))[::2]])
df=pd.DataFrame({'NEWcoL':my_list}).reset_index(names='idx')

Output :输出

   idx        NEWcoL
0    0  [00, 00, 0a]
1    1  [01, 0a, 0b]
2    2  [02, 01, 0c]
3    3  [03, 01, 0d]
4    4  [04, 02, 0e]
5    5  [05, 03, 0f]
6    6  [06, 03, 01]
7    7  [07, 04, 01]
8    8  [08, 05, 01]
9    9  [09, 05, 01]
import pandas as pd
df = pd.DataFrame(index=[0], columns=['NEWcoL'])
for i in range(0, 10):
    df.loc[i, 'NEWcoL']=[str(hex(i))[::2], str(hex(i*10))[::2], str(hex(i+10))[::2]]

df.index = np.arange(1, len(df) + 1)
df.index.names = ['idx']


           NEWcoL
idx              
1    [00, 00, 0a]
2    [01, 0a, 0b]
3    [02, 01, 0c]
4    [03, 01, 0d]
5    [04, 02, 0e]
6    [05, 03, 0f]
7    [06, 03, 01]
8    [07, 04, 01]
9    [08, 05, 01]
10   [09, 05, 01]

hex bulitin function already returns a string representation of an integer, no need to str additionally. hex bulitin 函数已经返回一个整数的字符串表示,不需要另外str Here's simple one-liner:这是简单的一行:

In [362]: df = pd.DataFrame({'idx':range(1, 11), 'newcol':[[hex(i)[::2], hex(i*10)[::2], hex(i+10)[::2]] for i in range(10)]})

In [363]: df
Out[363]: 
   idx        newcol
0    1  [00, 00, 0a]
1    2  [01, 0a, 0b]
2    3  [02, 01, 0c]
3    4  [03, 01, 0d]
4    5  [04, 02, 0e]
5    6  [05, 03, 0f]
6    7  [06, 03, 01]
7    8  [07, 04, 01]
8    9  [08, 05, 01]
9   10  [09, 05, 01]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将列表列表作为一行添加到数据帧,而每个列表都在一个单元格中? -Python - How to add a list of lists as one row to dataframe, with each list in one cell? - Python 将包含一个元素的列表插入 pandas dataframe 单元格 - Insert a list of containing one element into a pandas dataframe cell 如何相对于某些列为一个pandas数据框单元绘制图形 - how to plot graph for one pandas dataframe cell with respect to some columns 如何根据多个条件更改 Pandas Dataframe 中的一个单元格? - How to change one cell in a Pandas Dataframe based on multiple conditions? Pandas:如何将列表作为元素添加到 DataFrame 中的单个单元格 - Pandas : How to add a list as an element to a single cell in a DataFrame 熊猫如何将一个数据框的一列作为一行添加到另一数据框 - pandas how to add a column of one dataframe as a row into another dataframe Python Pandas Dataframe:如何同时将 append 索引到多个列表? - Python Pandas Dataframe: How to append more than one index to a list at the same time? 将矩阵放入一个熊猫DataFrame单元中 - putting matrix in one pandas DataFrame cell 尝试访问熊猫数据框中的一个单元格 - Trying to access one cell in a pandas dataframe 在一个熊猫数据框单元格中读取不同的行 - Read different lines in one pandas dataframe cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM