Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用动态列从dataframe获取值_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何使用动态列从dataframe获取值

Python 如何使用动态列从dataframe获取值,python,pandas,dataframe,Python,Pandas,Dataframe,Python新手在这里,我无法创建一个可以将某些列的值提取到另一种形式的函数。我曾多次尝试运行一个循环来获取数据,但我找不到一个好的pythonic方法来实现它。欢迎提供任何帮助或建议 PS:带有“Loaded with”的列包含加载了哪些项目的信息,但您也可以通过查看少数列的名称item_1L来获取此信息 我无法找到更好的方法来输入SO上的数据,因此我创建了一个 我需要单独项目的LBH,格式为 项目1=4.6x4.3x4.3 项目2=4.6x4.3x4.3或任何其他容易理解的方式 编辑:当我说

Python新手在这里,我无法创建一个可以将某些列的值提取到另一种形式的函数。我曾多次尝试运行一个循环来获取数据,但我找不到一个好的pythonic方法来实现它。欢迎提供任何帮助或建议

PS:带有“Loaded with”的列包含加载了哪些项目的信息,但您也可以通过查看少数列的名称item_1L来获取此信息

我无法找到更好的方法来输入SO上的数据,因此我创建了一个

我需要单独项目的LBH,格式为

项目1=4.6x4.3x4.3 项目2=4.6x4.3x4.3或任何其他容易理解的方式

编辑:当我说我需要4.6x4.3x4.3形式的答案时,我的意思是我需要的是“4.6x4.3x4.3”形式的答案,即不是数字的乘积。我需要这样的字符串格式:

字符串格式

index  Loadedwith  item_0L  item_0B  item_0H  item_1L  item_1B  item_1H    
1              01      4.6      4.3      4.3      4.6      4.3  4.3'
以下是我一直在尝试的:

def get_df (df):

    total_trucks = len(df)
    total_items = 0
    for i in range(len(df["Loaded with"])):
        total_items += len((df["Loaded with"].iloc[i]))



    for i in range(len(df["Loaded with"])):
        for j in range(total_items):
            for k in range(len((df["Loaded with"].iloc[i]))):
#                 pass
#                 print("value of i j k is {} {} {}".format(i,j,k))
                if(pd.isnull(Packed_trucks.loc["item_" + str(j) + "L"])):
                    display(Packed_trucks["item_" + str(j) + "L"])
#     return 0


get_df(Packed_trucks)

我有点困惑,所以如果这是一般性的,我很抱歉,但是看起来您要么需要解析数据,要么需要遍历数据。我推荐如下几点:

解析行 这将打开一个文件,读取数据,并根据数据形成一个列表。在这样做的过程中,遍历列表变得非常容易,从而使segway进入迭代

迭代 如果您需要遍历您的值列表,for循环是最简单的方法。如果您需要快速获取整行或整列,我建议您

data = [your data]
row = data[0][:]
column = data[:][0]
只要用所需的索引替换0即可注意:这只适用于二维列表,这就是为什么我建议如前所述进行解析。

编辑:您可以通过查看列表理解和列表拼接找到更多这样的示例

可能类似于:

m=df.loc[:,df.filter(like='item').columns]
df['Item1']=m.filter(like='0').astype(float).prod(axis=1)
df['Item2']=m.filter(like='1').astype(float).prod(axis=1)
输出:

    index   Loadedwith   item_0L    item_0B item_0H  item_1L    item_1B   item_1H   Item1   Item2
        1            1       4.6        4.3     4.3      4.6         4.3      4.3   85.054  85.054
编辑


此解决方案将利用
pd.melt
功能创建一个表,其中每一行都是卡车(索引)和项目编号的组合

df = pd.read_csv('df.csv')

# We will operate on a subset of columns, leaving just index and columns we need
truck_level_df = df.drop(['Name', 'TruckID', 'Length', 'Breadth', 
'Height', 'Volume', 'Weight', 'Price', 'Quantity', 'Loaded with'], 
axis = 1)
卡车水平高度:

       index  item_0L  item_0B  item_0H  item_1L  item_1B  item_1H
0      1      4.6      4.3      4.3      4.6      4.3      4.3
       index item_id_and_measure  item_val item_id
0      1                  0L       4.6       0
1      1                  0B       4.3       0
2      1                  0H       4.3       0
3      1                  1L       4.6       1
4      1                  1B       4.3       1
5      1                  1H       4.3       1
项目等级:

       index  item_0L  item_0B  item_0H  item_1L  item_1B  item_1H
0      1      4.6      4.3      4.3      4.6      4.3      4.3
       index item_id_and_measure  item_val item_id
0      1                  0L       4.6       0
1      1                  0B       4.3       0
2      1                  0H       4.3       0
3      1                  1L       4.6       1
4      1                  1B       4.3       1
5      1                  1H       4.3       1
最后一步:

item_measure_level_df['item_val'] = item_measure_level_df['item_val'].astype('str')

# Group by Item and get LxHxB string
item_level_df['volume_string'] = item_measure_level_df.sort_values(by = ['index','item_id_and_measure']).groupby(['index','item_id'])['item_val'].apply(lambda x: ' x '.join(x)).values
输出:

  index item_id    volume_string
0     1       0  4.3 x 4.3 x 4.6
3     1       1  4.3 x 4.3 x 4.6
此解决方案将消化尽可能多的列组


共享笔记本:

Hi Rohit,是否介意创建一个类似的示例Thanks@anky_91当然,谢谢你告诉我。嗯……谢谢你给了我新的方向,不过你能告诉我在哪里可能会出问题吗?@RohitKumar我会做一些优化。首先,尝试
pandas.read_csv
获取数据,然后查看
pandas.loc[]
下一步。我似乎无法运行您的代码,出现了一个错误replace()获取了一个意外的关键字参数“regex”。请用这个来尝试你自己的代码。colab正在使用pandas 0.22.0出于某种原因,0.23中引入了regex。这是有效的(我已经删除了regex参数并创建了一个regex模式):另外,我发布了一个解决方案,给你一个字符串,如下所示:4.3 x 4.3 x 4.6对不起,但我不想要值的浮点积,但是字符串值的格式类似于4.6x4.3x4.3,中间有字母x。我已经编辑了这个问题,很抱歉因为问题不清楚而浪费了时间。@RohitKumar编辑下的部分怎么样?谢谢你的回答,我们有没有办法让你说的
df['Item2']
的部分像df[Item+str(I)]之类的动态方式?我想你可以,但是,因为过滤器正在改变,可能会涉及到for循环。如果可能的话,您可以尝试为此发布新问题。:)@不幸的是,RohitKumar无法访问链接,我使用的是公司笔记本电脑垫,这就是原因
item_measure_level_df['item_val'] = item_measure_level_df['item_val'].astype('str')

# Group by Item and get LxHxB string
item_level_df['volume_string'] = item_measure_level_df.sort_values(by = ['index','item_id_and_measure']).groupby(['index','item_id'])['item_val'].apply(lambda x: ' x '.join(x)).values
  index item_id    volume_string
0     1       0  4.3 x 4.3 x 4.6
3     1       1  4.3 x 4.3 x 4.6